我正在将嵌套表单gem与rails 4一起使用。link_to_add可以完美地工作。 link_to_remove不起作用。它从dom中删除了新的嵌套形式,但实际上并没有删除该对象。我的模型是Campaign,我的嵌套表单来自一个名为配件的模型。
Campaign.rb
has_many :accessories, :dependent => :destroy
accepts_nested_attributes_for :accessories, :reject_if => :all_blank, allow_destroy: true
Accessory.rb
belongs_to :campaign
Campaigns_controller.rb
params.require(:campaign).permit(:product_name, :product_description, :accessories_attributes => [ :id, :name, :description, :_destroy ])
Application.js
//= require jquery
//= require jquery_ujs
//= require tinymce
//= require jquery_nested_form
//= require cosmo/bootswatch
//= require_self
new.html.erb
<%= nested_form_for @campaign, :multipart => true do |f| %>
<%= f.text_field :name %>
<%= render 'campaigns/accessories_fields', :f => f %>
<%= f.link_to_add "Add a accessory", :accessories %>
<% end %>
_accessories_fields.html.erb
<%= f.fields_for :accessories do |i| %>
<%= i.text_field :name %>
<%= i.link_to_remove "Remove this accessory" %>
<% end %>