简体   繁体   中英

Can't mass-assign protected attributes: _destroy - Ruby on Rails

I'm getting this error:

"ActiveModel::MassAssignmentSecurity::Error in DaysController#create

Can't mass-assign protected attributes: _destroy"

I didn't even know that _destroy is an attribute!

What I have going on:

My model is that I have "Trips" which has many "Days"

In the "Show" view of my Trips model, I'm rendering a partial for a Form to add a new "Day":

<div id="day_form">
  <%= render :partial => "day_form", :day => @day %>
</div> 

My model:

class Trip < ActiveRecord::Base
  attr_accessible :title, :days_attributes
  has_many :days
  accepts_nested_attributes_for :days, allow_destroy: true

end


 class Day < ActiveRecord::Base
  attr_accessible :activity_id, :order, :summary, :trip_id, :activities_attributes
  belongs_to :trip
  has_many :activities, :order => 'position'
  accepts_nested_attributes_for :activities, allow_destroy: true 
end

When I submit the form, I am getting this Mass Assignment error. Why?

EDIT

The 'Day' Form looks like this:

    <%= form_for(@day) do |f| %>

      <ul>
      <% @day.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

        <fieldset>
          <%= f.label :summary, "Day Summary" %><br />
          <%= f.text_area :summary, :rows => 1 %><br />
          <%= f.hidden_field :_destroy %>
              <%= link_to "remove", '#', class: "remove_fields" %>
        </fieldset>
        <div class="actions">
    <%= f.submit %>
        </div>
<% end %>
 module ApplicationHelper
  def link_to_remove_fields(name, f)
    text_field_tag(:_destroy) + link_to_function(name, "remove_fields(this)")
  end

replace f.hidden_field

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM