我正在使用 activeadmin (0.3.2) 和 nested_form (0.2.3)。
我的模型是:
class Event < ActiveRecord::Base
has_many :event_translations
attr_accessible :event_translations_attributes
accepts_nested_attributes_for :event_translations, :allow_destroy => true,
:reject_if => proc { |attributes|
attributes['title'].blank? and
attributes['description'].blank? and attributes['language_id'].blank?
end
EventTranslation < ActiveRecord::Base
belongs_to :event
belongs_to :language
attr_accessible :description, :title, :event_id, :language_id
end
class Language < ActiveRecord::Base
attr_accessible :iso, :name
end
在我的 active_admin 事件控制器中,表单呈现为部分:
form :partial => "form"
在我看来,我在 views/admin/events/_form.html.erb 下有以下内容:
<%= semantic_nested_form_for [:admin, @event] do |f|%>
<% f.object.event_translations.build %>
<%= f.semantic_fields_for :event_translations do |h| %>
<%=h.inputs "Translations" do %>
<%= h.input :language, :required => true, :as => :select, :prompt => "Select a Language", :collection => Language.all %>
<%= h.input :title, :label => "Name"%>
<%= h.input :description %>
<%= h.link_to_remove "Remove Translation" %>
<% end %>
<% end %>
<%= f.link_to_add "Add Translation", :event_translations %>
...
<% end %>
这可以很好地渲染字段,但是 link_to_add 和 link_to_remove 在点击时没有做任何事情。//= 需要将 jquery_nested_form 添加到 application.js 并且 nested_form gem 正确包含在 gemfile 中。
谢谢!