我对 ruby on rails 和编程都很陌生。我正在尝试开发一个应用程序,但我现在被困住了。我正在观看http://railscasts.com/episodes/196-nested-model-form-part-1制作嵌套模型表单,但我遇到了错误。我的问题详情如下;
我有雇主模型,雇主模型 has_many interviews 和 interview model has_many customquestions。我正在尝试创建一个表格,通过该表格我将收集信息以创建面试。虽然我做了所有必要的关联,但当我提交表单时,它会引发错误,说“Customquestions interview can't be blank”。我有点确定这是因为我错过了面试控制器中的一些代码。下面你可以看到我的面试控制器和我用来提交信息的表单模板。
面试控制员
class InterviewsController < ApplicationController
before_filter :signed_in_employer
def create
@interview = current_employer.interviews.build(params[:interview])
if @interview.save
flash[:success] = "Interview created!"
redirect_to @interview
else
render 'new'
end
end
def destroy
end
def show
@interview = Interview.find(params[:id])
end
def new
@interview = Interview.new
3.times do
customquestion = @interview.customquestions.build
end
end
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>
在面试模型中,我使用accepts_nested_attributes_for :customquestions
面试模式
class Interview < ActiveRecord::Base
attr_accessible :title, :welcome_message, :customquestions_attributes
belongs_to :employer
has_many :customquestions
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