0

我对编程和 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"}

我希望我已经为你们提供了足够的信息来了解该案例的问题所在。

先感谢您

4

1 回答 1

2

只需按照错误消息中的内容进行操作:尝试添加attr_accessible :customquestions_attributesInterview模型中:

class Interview < ActiveRecord::Base
   attr_accessible :title, :welcome_message, :customquestions_attributes
...
于 2012-06-09T20:01:59.817 回答