私は、Ruby on Rails とプログラミングの両方に本当に慣れていません。アプリケーションを開発しようとしていますが、今行き詰まっています。ネストされたモデル フォームを作成するためにhttp://railscasts.com/episodes/196-nested-model-form-part-1を見ていましたが、エラーが発生しています。私の問題の詳細は次のとおりです。
私は雇用者モデル、雇用者モデルには has_many のインタビュー、インタビュー モデルには has_many のカスタム質問があります。インタビューを作成するために情報を収集するフォームを作成しようとしています。必要なすべての関連付けを行いましたが、フォームを送信すると、「カスタム質問のインタビューを空白にすることはできません」というエラーが発生します。そのため、インタビューコントローラーのコードがいくつか欠けていると確信しています。以下に、私のインタビュー コントローラーと、情報を送信するために使用しているフォーム テンプレートを示します。
インタビューコントローラー
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