Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - :_destroy method not working

I am trying to use :_destroy method in a nested form, but it just does not work

There are two models:

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

In the controller I create a instance:

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

And the form looks like this:

<%= 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 %>

Also the function I use is here:

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

So the setting_items form is simply not working at all, It shows the update is successful, but nothing is actually added or deleted.

like image 941
Erik Avatar asked Nov 26 '25 05:11

Erik


1 Answers

for example->your model associations are as follows: just follow the below steps to make use of magical _destroy attribute

      ####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);

    })
like image 188
Milind Avatar answered Nov 27 '25 21:11

Milind



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!