我收到了这个错误:
“DaysMtroller#create
中的ActiveModel :: MassAssignmentSecurity :: Error无法批量分配受保护的属性:_destroy“
我甚至都不知道_destroy是一个属性!
我发生了什么:
我的模特是我有“旅行”,其中有很多“天”
在我的Trips模型的“显示”视图中,我正在渲染部分表单以添加新的“日”:
<div id="day_form">
<%= render :partial => "day_form", :day => @day %>
</div>
我的模特:
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
当我提交表单时,我收到此批量分配错误。为什么呢?
修改
'Day'表格如下:
<%= 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 %>
答案 0 :(得分:0)
module ApplicationHelper
def link_to_remove_fields(name, f)
text_field_tag(:_destroy) + link_to_function(name, "remove_fields(this)")
end
替换f.hidden_field