Rails:构建多模型表单

发布于 2024-12-20 04:34:24 字数 1780 浏览 0 评论 0原文

我有一个在数据库中保存一个问题和五个答案的表单,但我不知道如何保存答案,这是我的表单:我的

<%= 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弥枳 2024-12-27 04:34:24

您只会错过 Question 模型中的 accepts_nested_attributes_for :answers

请参阅文档

编辑:

您应该将 answers_attributes 添加到您的 attr_accessible 列表中

You only miss accepts_nested_attributes_for :answersin the Question model.

See doc.

EDIT:

You should add answers_attributes to your attr_accessible list

独自唱情﹋歌 2024-12-27 04:34:24

您应该看一下两个 RailsCast:

http://railscasts.com/ Episodes/196-nested-model-form-part-1http://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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文