我不确切知道这个问题的正确标题是什么,抱歉。
我有一个Question
模型,用户应该输入5个问题,然后在问题表中提交。
问题是它只提交一个问题,这是最后一个问题。
如何让它同时提交所有问题字段?
的观点:
<% question_numbering = 0 %>
<%= simple_form_for(@quiz, html: {class: 'form-vertical' }) do |f| %>
<%= render 'shared/error_messages_question' %>
<div>
<%= %>
question <%= question_numbering += 1 %><br>
<%= f.input_field :content, :rows => 3, :style => "width:80%", :placeholder => "enter your question." %><br>
question <%= question_numbering += 1 %><br>
<%= f.input_field :content, :rows => 3, :style => "width:80%", :placeholder => "enter your question." %><br>
question <%= question_numbering += 1 %><br>
<%= f.input_field :content, :rows => 3, :style => "width:80%", :placeholder => "enter your question." %><br>
question <%= question_numbering += 1 %><br>
<%= f.input_field :content, :rows => 3, :style => "width:80%", :placeholder => "enter your question." %><br>
question <%= question_numbering += 1 %><br>
<%= f.input_field :content, :rows => 3, :style => "width:80%", :placeholder => "enter your question." %><br>
</div>
<%= f.submit 'Submit', :class => "btn btn-default" %>
<% end %>
控制器:
class QuestionsController < ApplicationController
def index
@quiz = Question.new
@questioner = Questioner.new
end
def new
@quiz = Question.new(quiz_params)
end
def show
@quiz = Question.find(params[:id])
end
def edit
@quiz = find(params[:id])
raise "Question Not edited!" unless @quiz
end
def create
@quiz = Question.new(quiz_params)
if @quiz.save
flash[:warning] = 'You have successfully posted the questions!'
redirect_to questions_path
else
flash[:error] = "Please review the problems below."
# render 'new'
redirect_to questions_path
end
end
private
def quiz_params
params.require(:question).permit(:content, :answered, :questioner_id, :category_id)
end
end
答案 0 :(得分:1)
您目前正在使用问题资源 - 一次创建一个问题才真正有意义,因为它们不作为集合存在,而是作为单独的单元存在。
如果您有其他资源,例如Quiz,它有很多问题 - 那么一次创建多个问题是有意义的。
您需要将测验创建为与问题有has_many
关联的单独资源。
查看this great Railscast on nested forms哪个可以帮到你(这是我开始学习Rails的地方)。