3

我正在尝试以嵌套形式使用 :_destroy 方法,但它不起作用

有两种型号:

class Setting < ActiveRecord::Base
  attr_accessible :category, :name, :setting_items_attributes, :_destroy
  attr_accessor :_destroy

  has_many :setting_items, :dependent => :destroy
  accepts_nested_attributes_for :setting_items, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end

class SettingItem < ActiveRecord::Base
  attr_accessible :setting_id, :value
  belongs_to :setting
end

在控制器中,我创建了一个实例:

  def edit
    @setting = Setting.find(params[:id])
    @setting.setting_items.build
  end

表格如下所示:

<%= form_for(@setting) do |f| %>
  <div class="field">
    <%= f.label :category %>
    <%= f.text_field :category %>
  </div>
  <div class="field">
    <%= f.label :name %>
    <%= f.text_field :name %>
  </div>
  <hr>
  <h3>Params:</h3>
    <%= f.fields_for :setting_items do |s| %>
        <span>
        <div class="fields">
            <%= s.text_field :value %>
            <%= s.hidden_field :_destroy %>
            <%= link_to_function "delete", "remove_fields(this)"%>
        </div>      
    <% end %>    
  <div class="actions">
    <%= f.submit "Update", :class => "btn btn-primary"%>
  </div>
<% end %>

我使用的功能也在这里:

function remove_fields(link){
    $(link).prev("input[type=hidden]").val("1");
    $(link).parent().fadeOut("slow");
}

所以 setting_items 表单根本不起作用,它显示更新成功,但实际上没有添加或删除任何内容。

4

1 回答 1

2

例如->您的模型关联如下:只需按照以下步骤使用神奇的_destroy 属性

      ####parent model
            plan.rb

             has_many :members,:dependent => :destroy
             #this is important you want to destroy nested records
             accepts_nested_attributes_for:members,:allow_destroy => true 

             attr_accessible :members_attributes


        ##child model
            member.rb

            belongs_to :plan


    ######controller

    def edit
    @plan.Plan.find(params[:id])

    end


    #####edit.html.erb

    <%= form_for @plan do |f| %>

     <%= f.fields_for :members do |member| %>
    <div class="member">
      <%= member.text_field :title%>
      <%= image_tag 'delete.png',:class =>'remove_member',:id=>member.id %>
      <!-- we need to set this hidden field value as 1 to mark it to be deleted during save/update       of  parent-->
      <%= member.hidden_field :_destroy, :class => 'delete_member', :member_id => member.id %>
      <!--similar to 
      <input id="plan_members_attributes_0__destroy" class="delete_member" type="hidden" value="false" name="plan[members_attributes][0][_destroy]" member_id="#{id of member}">
 -->
    </div>
     <%end%>
    <%end%>


    ##add js onclick of remove button/image

    $('.delete_member').click(function(){
        //remove div from screen
        $(this).closest('.member').remove();
         //get relevant id to remove/mark as delete
         id =jQuery(this).attr('id');
         //remove/mark the nested model/record as ready for deletion for rails by adding true/1     value
         $("input[member_id="+id+"]").attr('value',1);

    })
于 2014-03-14T14:42:33.923 回答