0
我对轨道上的编程和ruby都很陌生。我跟着http://ruby.railstutorial.org/,然后我开始从http://railscasts.com开始观看剧集。我想要做的是“以单一形式处理多个模型”。下面你会看到我的模型和他们的协助,以及我试图从用户那里获取信息的形式观点。使用嵌套属性时的质量分配警告
我的造型就是这样;
有雇主,雇主有面试和面试有问题。
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"}
我希望我已经提供了足够的信息,你们要明白什么是这种情况下的问题。
预先感谢您
问题解决了谢谢,但现在新的问题发生。新问题与此无关,但也许你可能对此有所了解。在InterviewController中创建动作并不会获取面试的ID并将其放入customquestions的数据库表中。我目前为访谈控制器创建的动作如下,我知道我应该添加一些内容:'def create @interview = current_employer.interviews.build(params [:interview]) if @acsces.save flash [:成功] =“采访创建!” redirect_to @interview else render'new' end end'@ mikhail-d – hayri
@hayri,对不起,我在这里找不到错误。也许我不明白这个问题......如果你坚持不懈 - 随时提出一个新问题。 –
我真的很担心,现在当我提交表格时,虽然我填写了所有字段,但它引发了一个问题,说“Customquestions采访不能为空”。我相信这是因为Interview控制器中缺少一些代码。正如我在最新评论中发布的,您可以在Interview Controller中看到我的创建操作,并且我觉得应该为该创建操作添加一些内容,以使customquestions的数据库表可以访问interview_id。现在创建动作并不能获得interview_id,并且会引发错误“Customquestions采访不能为空”。 – hayri