Car*_*ten 19 ruby-on-rails belongs-to ruby-on-rails-3 activeadmin
我们使用active_admin作为管理后端.
我们有一个模型"App":belongs_to model"Publisher":
class App < ActiveRecord::Base
belongs_to :publisher
end
class Publisher < ActiveRecord::Base
has_many :apps
end
Run Code Online (Sandbox Code Playgroud)
为"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
Run Code Online (Sandbox Code Playgroud)
经过几个小时的搜索,我会感激任何暗示......谢谢!
首先,确保在Publisher模型中您拥有关联对象的正确权限:
class App < ActiveRecord::Base
attr_accessible :publisher_attributes
belongs_to :publisher
accepts_nested_attributes_for :publisher, reject_if: :all_blank
end
Run Code Online (Sandbox Code Playgroud)
然后在您的ActiveAdmin文件中:
form do |f|
f.inputs do
f.input :title
# ...
end
f.inputs do
# Output the collection to select from the existing publishers
f.input :publisher # It's that simple :)
# Then the form to create a new one
f.object.publisher.build # Needed to create the new instance
f.semantic_fields_for :publisher do |p|
p.input :name
end
end
f.buttons
end
Run Code Online (Sandbox Code Playgroud)
我在我的应用程序中使用了稍微不同的设置(而不是has_and_belongs_to_many关系),但我设法让它为我工作.如果此代码输出任何错误,请告诉我.
小智 7
该form_builder类支持调用的方法has_many.
f.inputs do
f.has_many :publisher do |p|
p.input :name
end
end
Run Code Online (Sandbox Code Playgroud)
那应该做的.
更新:我重新阅读了您的问题,这只允许添加新的发布者,我不知道如何进行选择或创建.
根据ActiveAdmin:http://activeadmin.info/docs/5-forms.html
你只需要做如下:
f.input :publisher
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5657 次 |
| 最近记录: |