Reputation: 3035
In the following code, how does one populate a prefilled form field with a value from another model into the ActiveAdmin form:
ActiveAdmin.register Person do
index do
column :forename
column :surname
column :code do |p|
MyCode.find_by_person_id(p.id).code
end
column :updated_at
default_actions
end
filter :forename
filter :surname
form do |f|
f.inputs "Person" do
f.input :forename
f.input :surname
# How to get a value here, e.g. MyCode.find_by_person_id(p.id).code as above
#f.input :code, :input_html => { :value => value??? }, as: :hidden
end
f.actions
end
end
Upvotes: 3
Views: 19205
Reputation: 3035
As nistvan commented above, has_one
relationship fetches the related object into the form object, thus my problem could be solved with:
ActiveAdmin.register Person do
index do
column :forename
column :surname
column :code do |p|
MyCode.find_by_person_id(p.id).code
end
column :updated_at
default_actions
end
filter :forename
filter :surname
form do |f|
f.inputs "Person" do
f.input :forename
f.input :surname
f.input :code, :input_html => { :value => f.object.my_code.code }, as: :hidden
end
f.actions
end
end
Upvotes: 15
Reputation: 2970
If in your model a Person has_one Code, then f.input :code
will work fine. Also you don't have to explicit determine the Code in the index block, just: column :code
http://guides.rubyonrails.org/association_basics.html#the-has-one-association
Upvotes: 1