使用嵌套的属性时,质量分配警告(Mass assignment warning when using

2019-10-31 18:41发布

我很新的编程和Ruby on Rails的。 我按照http://ruby.railstutorial.org/然后我开始观看情节http://railscasts.com 。 我所试图做的是,“处理多个模型的单一形式”。 下面你将看到我的模型和它们assosications还我试图来获取用户的信息形式的视图。

我的造型是;

有雇主,雇主面谈和面试有疑问。

Customquestion模型:

class Customquestion < ActiveRecord::Base
  attr_accessible :content
  belongs_to :interview

  validates :content, length: {maximum: 300}
  validates :interview_id, presence: true
end

面试模式:

class Interview < ActiveRecord::Base
  attr_accessible :title, :welcome_message
  belongs_to :employer
  has_many :customquestions, dependent: :destroy
  accepts_nested_attributes_for :customquestions

  validates :title, presence: true, length: { maximum: 150 }
  validates :welcome_message, presence: true, length: { maximum: 600 }
  validates :employer_id, presence: true
  default_scope order: 'interviews.created_at DESC'
end

窗体创建新的采访;

<%= provide(:title, 'Create a new interview') %>
<h1>Create New Interview</h1>

<div class="row">
  <div class="span6 offset3">
    <%= form_for(@interview) do |f| %>
    <%= render 'shared/error_messages_interviews' %>

      <%= f.label :title, "Tıtle for Interview" %>
      <%= f.text_field :title %>

      <%= f.label :welcome_message, "Welcome Message for Candidates" %>
      <%= f.text_area :welcome_message, rows: 3 %>

      <%= f.fields_for :customquestions do |builder| %>
        <%= builder.label :content, "Question" %><br />
        <%= builder.text_area :content, :rows => 3 %>
      <% end %>
      <%= f.submit "Create Interview", class: "btn btn-large btn-primary" %>
    <% end %>
  </div>
</div>

当我填写与所需信息的形式和提交,我得到下面的错误;

Can't mass-assign protected attributes: customquestions_attributes

Application Trace | Framework Trace | Full Trace
app/controllers/interviews_controller.rb:5:in `create'
Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"cJuBNzehDbb5A1Zb14BjBfz1eOsjBCDzGhYKT7q6A0k=",
 "interview"=>{"title"=>"",
 "welcome_message"=>"",
 "customquestions_attributes"=>{"0"=>{"content"=>""}}},
 "commit"=>"Create Interview"}

我希望我已经提供了足够的信息,你们要明白什么是这种情况下的问题。

先感谢您

Answer 1:

只要按照写的是什么错误消息:尝试添加attr_accessible :customquestions_attributesInterview模式:

class Interview < ActiveRecord::Base
   attr_accessible :title, :welcome_message, :customquestions_attributes
...


文章来源: Mass assignment warning when using nested attributes