Ain*_*vri 2 ruby ruby-on-rails activeadmin rails-activerecord
在以下代码中,如何使用另一个模型中的值将预填充的表单字段填充到ActiveAdmin表单中:
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
Run Code Online (Sandbox Code Playgroud)
Ain*_*vri 11
正如nistvan上面评论的那样,has_one关系将相关对象提取到表单对象中,因此我的问题可以通过以下方式解决:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13168 次 |
| 最近记录: |