Rails:构建多模型表单
我有一个在数据库中保存一个问题和五个答案的表单,但我不知道如何保存答案,这是我的表单:我的
<%= 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
我应该做什么来保存问题和数据库中的答案?
谢谢!!
I have a form that save a question and five answers in the database but I don't know how I can save the answers, this is my form:
<%= 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 %>
My models:
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
And my "Question" controller:
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
What I should do to save question and answer in the database?
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只会错过
Question
模型中的accepts_nested_attributes_for :answers
。请参阅文档。
编辑:
您应该将
answers_attributes
添加到您的attr_accessible
列表中You only miss
accepts_nested_attributes_for :answers
in theQuestion
model.See doc.
EDIT:
You should add
answers_attributes
to yourattr_accessible
list您应该看一下两个 RailsCast:
http://railscasts.com/ Episodes/196-nested-model-form-part-1 和 http://railscasts.com/episodes/197-nested-model-form-part-2
它们可能会对您有很大帮助!
这些演员背后的人 Ryan Bates 创造了一个伟大的宝石来处理嵌套形式!
You should take a look at two RailsCasts:
http://railscasts.com/episodes/196-nested-model-form-part-1 and http://railscasts.com/episodes/197-nested-model-form-part-2
They might help you a lot!
The man behind those casts, Ryan Bates, created a great gem to handle nested forms!