Rails - :_destroy方法不起作用

时间:2013-06-25 03:39:40

标签: ruby-on-rails nested-attributes hidden-field destroy

我试图在嵌套表单中使用:_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表单根本不起作用,它显示更新成功,但实际上没有添加或删除。

1 个答案:

答案 0 :(得分:2)

例如&gt;您的模型关联如下: 只需按照以下步骤使用 魔法 < / strong> _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);

    })