简体   繁体   中英

ActiveAdmin update two models in one form

I have an ActiveAdmin page to edit Loan info, like that:

ActiveAdmin.register Loan do
  ...
  form do |f|
  f.inputs 'Loan' do
    f.input :name
    f.input :amount
  end
  f.actions
end

(I'm omitting lot of fields for clarity)

Now, due to performance reasons I extracted amount field to LoanDetails model that has one-to-one relation with Loan

class Loan < ActiveRecord::Base
  ...
  has_one :details, class_name: 'LoanDetails', foreign_key: :loan_id
  ...
end

class LoanDetails < ActiveRecord::Base
  ...
  belongs_to :loan
  ...
end

This change brakes ActiveAdmin page. Attempt to edit loan results in

unknown attribute 'amount' for Loan.

error.

How can I update my ActiveAdmin to work properly with new data structure?

My colleague helped me to solve this while I was in the middle of writing StackOverflow question.

ActiveAdmin.register Loan do
  ...
  form do |f|
    f.inputs 'Loan' do
      f.input :name
    end

    f.inputs 'Loan Details', for: [:details, f.object.details] do |d|
      d.input :amount
    end

    f.actions
  end
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