简体   繁体   中英

How to create associated object by link click in Activeadmin index page?

I have the relations:

user.rb

has_one :account

account.rb

  has_many :transactions
  belongs_to :user

transaction.rb

belongs_to :account

How to make a link in Activeadmin on User index page to create a transaction for this user? Sorry if I wasn't very clear with my question.

UPDATE

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

rake routes

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

UPDATE2 I move it to index as I wanted at start

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

I've got the error:

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

Sure it's undefined, User has_one Account. Something wrong with relations. And I think even if this thing will start to work, it will not to be what I want. By me I need something like new_admin_user_account_transaction_path(user.id)

If you nest transactions under user, you could point the link to:

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

By nesting I meant:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM