我不断收到这个错误,我已经做模型入店的属性:Rails的:不能大规模指派保护的属性:奖品,optininpartner
class Contest < ActiveRecord::Base
attr_accessible :optinpartners_attributes, :prizes_attributes, :css, :description, :enddate, :promotion, :rules, :slug, :startdate, :title
accepts_nested_attributes_for :optinpartners
accepts_nested_attributes_for :prizes
has_many :contest_entries
has_many :contestants, :through => :contest_entries
has_many :contest_prizes
has_many :prizes, :through => :contest_prizes
has_many :contest_optins
has_many :optinpartners, :through => :contest_optins
确切的错误是:
Can't mass-assign protected attributes: prize, optininpartner
app/controllers/contests_controller.rb:49:in `new'
app/controllers/contests_controller.rb:49:in `create'
根据我的理解:如果该属性在模型中可访问,这应该不成问题。但是,大规模分配也是一个安全漏洞,所以如何在不牺牲安全性的情况下解决这个问题?
编辑: 由于要求:这是Contest的_form.erb.html文件。而这个片段是奖品和加入项目
<h2> Enter information on prizes </h2>
<%= f.fields_for :prize do |builder| %>
<%= builder.label :prize, "Prize" %><br/>
<%= builder.text_field :prize%><br/>
<br/>
<%= builder.label :description, "Description" %>
<%= builder.text_field :description%>
<%end%>
<hr>
<hr>
<h2> Enter information on Opt-In Partners </h2>
<%= f.fields_for :optinpartner do |builder| %>
<%= builder.label :name, "Name of Partner" %>
<%= builder.text_field :name%>
<%end%>
<hr>
你已经做了attributes_accessible,但不是实际的对象,如果你做'attr_accessible:optinpartners'它应该按预期工作。 – Doon 2013-05-01 15:16:30
你可以发布传递给'create'动作的视图表单代码吗? – PinnyM 2013-05-01 15:16:41
将':prize'和':optininpartner'添加到'attr_accessible'。 – 2013-05-01 15:17:13