嵌套表单gem可用于添加,但不适用于rails 4中的删除

时间:2019-02-13 04:35:05

标签: ruby-on-rails ruby-on-rails-4

我正在将嵌套表单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 %>

1 个答案:

答案 0 :(得分:0)

here所述,您需要添加

//= require jquery_nested_form

到您的application.js文件