使用ActiveAdmin(0.5.1)我想创建多个不同的表单来创建对象并将其保存到我的数据库中。我试图通过使用ActiveAdmin.register_page
来做到这一点,但我在尝试创建表单时遇到了麻烦。似乎在使用register_page
时,您没有获得与form
调用中相同的register
方法。这是代码:
ActiveAdmin.register_page "New Object" do
content do
para "Here you can create new objects!"
para "This content will be replaced with links to the specialized forms"
end
end
其中一种形式的代码:
ActiveAdmin.register_page "Type 1" do
menu :label => "Type 1", :parent => "New Object"
content do
panel "Attributes" do
form do |f|
f.input :color
f.input :size
end
end
end
但是,此表单不会以任何可行的方式呈现。此外,f.inputs
以及您在示例中可以看到的许多其他方法(如this)也不起作用。是否可以使用ActiveAdmin#register_page
生成功能完整的表单?
答案 0 :(得分:2)
要在非标准上下文中创建表单(即,不是资源注册表),您需要使用formtastic的semantic_form_for
并指定:url
和:builder
选项。
content do
semantic_form_for MyObject.new, :url => admin_my_objects_url, :builder => ActiveAdmin::FormBuilder do |f|
f.inputs "My Object" do
f.input :color
f.input :size
end
f.actions
end
end
这将为您提供一个标有“我的对象”的面板,其中包含您的对象的表单,其下方有一个提交按钮。
答案 1 :(得分:0)
foreachPartition
在自定义ActiveAdmin页面中,您有一个工作表的示例。添加
非常重要ActiveAdmin.register_page "Families Placement" do
menu label: "Populaire Familles"
page_action :update, method: :post do
Family.find(params['id']).update_attributes(id_1: params['id_1'], id_2: params['id_2'])
redirect_to "/"
end
content do
Family.all.order(id: :asc).each do |family|
form action: "families_placement/update", method: :post do |f|
columns do
panel family.name do
f.input :id, type: :hidden, value: family.id, name: 'id'
f.input :id_1, as: :select, collection: collect_posts, value: family.id_1, name: 'id_1'
f.input :id_2, as: :select, collection: collect_posts, value: family.id_2, name: 'id_2'
f.input :authenticity_token, type: :hidden, name: :authenticity_token, value: form_authenticity_token
f.input :submit, type: :submit
end
end
end
end
end
end
在这里我找到了所有要做的信息:https://asafdav2.github.io/2016/adding-forms-to-activeadmin-custom-pages/