active_admin 中的嵌套表单,带有选择或创建选项
我们使用 active_admin 作为我们的管理后端。
我们有一个模型“App”:属于模型“Publisher”:
class App < ActiveRecord::Base
belongs_to :publisher
end
class Publisher < ActiveRecord::Base
has_many :apps
end
为“App”模型创建新条目时,我希望可以选择选择现有发布者或(如果尚未创建发布者)创建以相同(嵌套)形式(或至少不离开页面)的新发布者。
有没有办法在 active_admin 中执行此操作?
这是我们到目前为止所得到的(在 admin/app.rb 中):
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs do
f.input :title
...
end
f.inputs do
f.semantic_fields_for :publisher do |p| # this is for has_many assocs, right?
p.input :name
end
end
f.buttons
end
经过几个小时的搜索,我将不胜感激任何提示......谢谢!
We are using active_admin for our administration backend.
We have a model "App" that :belongs_to model "Publisher":
class App < ActiveRecord::Base
belongs_to :publisher
end
class Publisher < ActiveRecord::Base
has_many :apps
end
When creating a new entry for the "App" model I want to have the option to either select an existing publisher or (if the publisher is not yet created) to create a new publisher in the same (nested) form (or at least without leaving the page).
Is there a way to do this in active_admin?
Here's what we have so far (in admin/app.rb):
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs do
f.input :title
...
end
f.inputs do
f.semantic_fields_for :publisher do |p| # this is for has_many assocs, right?
p.input :name
end
end
f.buttons
end
After hours of searching, I'd appreciate any hint... Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,确保在您的发布者模型中您拥有关联对象的正确权限:
然后在您的 ActiveAdmin 文件中:
我在我的应用程序中使用了稍微不同的设置(而是 has_and_belongs_to_many 关系),但我设法让它工作为我。如果此代码输出任何错误,请告诉我。
First, make sure that in your Publisher model you have the right permissions for the associated object:
Then in your ActiveAdmin file:
I'm using a slightly different setup in my app (a has_and_belongs_to_many relationship instead), but I managed to get it working for me. Let me know if this code outputs any errors.
form_builder 类支持名为
has_many
的方法。那应该可以完成工作。
更新:我重新阅读了您的问题,这只允许添加新的发布者,但我不确定如何进行选择或创建。
The form_builder class supports a method called
has_many
.That should do the job.
Update: I re-read your question and this only allows to add a new publisher, I am not sure how to have a select or create though.
根据 ActiveAdmin:http://activeadmin.info/docs/5-forms.html
您只需要执行以下操作:
According to ActiveAdmin: http://activeadmin.info/docs/5-forms.html
You just need to do as below:
我发现你需要做三件事。
为表单添加语义字段
将nested_belongs_to语句添加到控制器
更新控制器上允许的参数以接受参数,使用关键字属性
I've found you need to do 3 things.
Add semantic fields for the form
Add a nested_belongs_to statement to the controller
Update your permitted parameters on the controller to accept the parameters, using the keyword attributes