使用嵌套属性时的质量分配警告 [英] Mass assignment warning when using nested attributes

查看:70
本文介绍了使用嵌套属性时的质量分配警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Rails编程和ruby都是新手.我已经关注了 http://ruby.railstutorial.org/,然后我开始观看 http://railscasts.com .我正在尝试做的是以一种形式处理多个模型".在下面,您将看到我的模型及其关联,以及我试图从用户那里获取信息的表格视图.

I am quite new to both programming and ruby on rails. I have followed http://ruby.railstutorial.org/ and then i have started to watch episodes from http://railscasts.com. What i am trying to do is that "Handling multiple models in a single form". Below you will see my models and their assosications and also the view of form that i am trying to get info from users.

我的建模就是那个

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

There are employers, employers have interviews and interviews have questions.

自定义问题模型:

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

用于创建新采访的表格;

Form to create new interview;

<%= 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>

当我用所需信息填写表格并提交时,出现以下错误;

When i fill the form with required information and submit it, i get following error;

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

我希望我能为你们提供足够的信息,以了解该案的问题所在.

I hope that i have provided enough information for you guys to understand what is problem with that case.

提前谢谢

推荐答案

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

Just follow what is written in the error message: try to add attr_accessible :customquestions_attributes to Interview model:

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

这篇关于使用嵌套属性时的质量分配警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆