简体   繁体   English

无法批量分配受保护的属性:_destroy-Ruby on Rails

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

I'm getting this error: 我收到此错误:

"ActiveModel::MassAssignmentSecurity::Error in DaysController#create “ DaysController#create中的ActiveModel :: MassAssignmentSecurity :: Error

Can't mass-assign protected attributes: _destroy" 无法批量分配受保护的属性:_destroy”

I didn't even know that _destroy is an attribute! 我什至不知道_destroy是一个属性!

What I have going on: 我正在做什么:

My model is that I have "Trips" which has many "Days" 我的模型是我有很多天的“ Trips”

In the "Show" view of my Trips model, I'm rendering a partial for a Form to add a new "Day": 在我的Trips模型的“显示”视图中,我正在渲染表单的部分内容以添加新的“日期”:

<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 替换f.hidden_field

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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