我们使用active_admin作为我们的管理后端。
我们有一个:belongs_to模型“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
经过几个小时的搜寻,如果有任何提示我将不胜感激。谢谢!
发布于 2012-11-02 06:11:07
首先,确保在您的Publisher模型中,您对关联的对象具有正确的权限:
class App < ActiveRecord::Base
attr_accessible :publisher_attributes
belongs_to :publisher
accepts_nested_attributes_for :publisher, reject_if: :all_blank
end
然后在您的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
我在我的应用程序中使用了一个稍微不同的设置(改为has_and_belongs_to_many关系),但我设法让它为我工作。如果此代码输出任何错误,请让我知道。
发布于 2011-09-06 12:55:39
form_builder类支持名为has_many
的方法。
f.inputs do
f.has_many :publisher do |p|
p.input :name
end
end
这应该可以完成这项工作。
更新:我重新阅读了你的问题,这只允许添加一个新的发布者,我不确定如何有一个选择或创建。
发布于 2012-08-16 17:59:51
https://stackoverflow.com/questions/7198062
复制相似问题