我对rails上的编程和ruby都很陌生。我已关注http://ruby.railstutorial.org/,然后我开始观看http://railscasts.com的剧集。我想要做的是“以单一形式处理多个模型”。下面你将看到我的模型和他们的assosications以及我试图从用户获取信息的表单视图。
我的模特是那个;
有雇主,雇主有面试和面试有问题。
Customquestion model:
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"}
我希望我已经为你们提供了足够的信息来了解这个案例的问题。
提前谢谢
答案 0 :(得分:2)
只需按照错误消息中的内容操作:尝试将attr_accessible :customquestions_attributes
添加到Interview
型号:
class Interview < ActiveRecord::Base
attr_accessible :title, :welcome_message, :customquestions_attributes
...