У меня есть форма для сохранения вопроса и пять ответов в базе данных, но я не знаю, как мне сохранить ответы, это моя форма:
<%= form_for([:admin, @question]) do |f| %>
...
<%= f.fields_for :answers do |builder| %>
<%= builder.label :answer, "Risposta", :class => "v-align" %>
<%= builder.text_field :answer, :rows => 2 %>
<%= builder.label :correct, "Corretta", :class => "v-align" %>
<%= builder.check_box :correct %>
<% end %>
...
<% end %>
Мои модели:
class Question < ActiveRecord::Base
has_many :answers
accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
attr_accessible :answers_attributes, :quiz_id, :question, :sort_order, :point_value, :number_correct, :explanation
end
class Answer < ActiveRecord::Base
belongs_to :question
attr_accessible :question_id, :answer, :correct, :sort_order
end
И мой контроллер "Вопрос":
def new
@question = Question.new
5.times { @question.answers.build }
respond_to do |format|
format.html # new.html.erb
format.json { render :json => @question }
end
end
def create
@question = Question.new(params[:question])
respond_to do |format|
if @question.save
format.html { redirect_to admin_question_path(@question), :notice => 'Test was successfully created.' }
format.json { render :json => @question, :status => :created, :location => @question }
else
format.html { render :action => "new" }
format.json { render :json => @question.errors, :status => :unprocessable_entity }
end
end
end
Что мне нужно сделать, чтобы сохранить вопрос и ответ в базе данных?
Спасибо !!