【问题标题】:How to create associated object by link click in Activeadmin index page?如何通过 Activeadmin 索引页面中的链接单击创建关联对象?
【发布时间】:2016-02-04 17:49:18
【问题描述】:

我有关系:

user.rb

has_one :account

account.rb

  has_many :transactions
  belongs_to :user

transaction.rb

belongs_to :account

如何在用户索引页面上的 Activeadmin 中创建一个链接来为该用户创建交易?抱歉,如果我的问题不是很清楚。

更新

admin/user.rb

show do
      panel 'Платежи' do
        table_for user.account do
          column('accaunt', :acc_number)
          column('balance', :balance)
          column('Last payment date', :last_pay_date)
          column('Next payment date', :next_pay_date)
          column(link_to 'Add some money', new_admin_user_account)
        end
      end
  end

搜索路线

new_admin_user_account GET        /admin/users/:user_id/accounts/new(.:format)              admin/accounts#new
             edit_admin_user_account GET        /admin/users/:user_id/accounts/:id/edit(.:format)         admin/accounts#edit
                  admin_user_account GET        /admin/users/:user_id/accounts/:id(.:format)              admin/accounts#show

更新2 我在开始时将它移动到我想要的索引

index do
    id_column
    column :email
    column :uid
    column :username
    column :email_notify
    column :msg_notify
    column :blocked
    column do |user|
      link_to 'Add some money', new_admin_user_account_path(user.id)
    end
    actions
  end

我遇到了错误:

undefined method 'accounts' for #<User:0x000000139eb2c8>

确定是未定义的,用户拥有一个帐户。关系有问题。而且我认为即使这件事开始起作用,它也不会是我想要的。我需要类似new_admin_user_account_transaction_path(user.id)

【问题讨论】:

  • 你在末尾缺少_path 和用户参数:new_admin_user_account_path(user)
  • 你能试试column(:add) { |account| link_to 'Add some money', new_admin_user_account_path(account.user) }我不确定是否需要:add
  • 是的...我已经找到了。肯定为时已晚。但它出现了新的错误(
  • 请使用最新信息更新您的问题。
  • 很好,因为用户只有一个帐号。请参阅我更新的答案,您想为用户帐户创建交易,因此您的嵌套应该更改以反映这一点。

标签: ruby-on-rails activeadmin


【解决方案1】:

如果您在用户下嵌套交易,您可以将链接指向:

column do |user|
  link_to 'Add some money', new_admin_account_transaction_path(user.account)
end

我的意思是嵌套:

ActiveAdmin.register Transaction do
  belongs_to :account
  ...
end

【讨论】:

  • 是的,我以前做过,但我有错误undefined local variable or method new_admin_user_account'`。
  • 你注册账号了吗,就像我给你看的那样? rake routes | grep admin 的输出是什么。对于什么对象,您收到未定义的方法错误,我的意思是您能否粘贴整个错误消息。
  • 是的,我做到了。我可以在 rake routes 输出中看到这条路线
  • 如果你看到路由,方法存在,你可能在错误的对象上调用它。向我们展示(更新问题)您拥有链接的视图片段、路线(来自rake routes)和整个错误。
猜你喜欢
  • 1970-01-01
  • 2019-04-25
  • 1970-01-01
  • 2019-08-20
  • 1970-01-01
  • 2016-03-30
  • 1970-01-01
  • 2021-09-25
  • 1970-01-01
相关资源
最近更新 更多