【问题标题】:Mass assignment warning when using nested attributes使用嵌套属性时的质量分配警告
【发布时间】:2012-06-09 19:25:43
【问题描述】:

我对编程和 ruby​​ on rails 都很陌生。我关注了http://ruby.railstutorial.org/,然后我开始观看http://railscasts.com 的剧集。我想做的是“以一种形式处理多个模型”。下面您将看到我的模型及其关联,以及我试图从用户那里获取信息的表单视图。

我的造型是这样的;

有雇主,雇主有面试,面试有问题。

自定义问题模型:

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"}

我希望我已经提供了足够的信息让你们了解这个案例的问题。

提前谢谢你

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 nested-attributes


    【解决方案1】:

    只需按照错误消息中的内容:尝试将attr_accessible :customquestions_attributes添加到Interview模型:

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

    【讨论】:

    • 已解决问题,谢谢,但现在又出现了新问题。新问题与此无关,但也许您可能对此有所了解。在 InterviewController 中创建操作不会获取面试的 id 并将其放入自定义问题的数据库表中。我当前为面试控制器创建操作如下,我知道我应该添加一些内容:` def create @interview = current_employer.interviews.build(params[:interview]) if @interview.save flash[:success] = “采访已创建!” redirect_to @interview else render 'new' end end `@mikhail-d
    • @hayri,对不起,我在这里找不到错误。也许我不明白这个问题......如果你坚持下去 - 随时提出一个新问题。
    • 我真的很坚持,现在当我提交表格时,虽然我填写了所有字段,但它提出了一个问题,即“Customquestions interview can't be blank”。我确信这是因为面试控制器中缺少一些代码。正如我在上一条评论中发布的那样,您可以在 Interview Controller 中看到我的创建操作,我觉得我应该在该创建操作中添加一些内容,以便自定义问题的数据库表可以访问 interview_id。现在创建操作没有得到 interview_id,它会引发错误“Customquestions interview can't be blank”。
    猜你喜欢
    • 2011-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    • 2012-03-08
    相关资源
    最近更新 更多