想要创建一个轮询应用程序,允许您创建一个包含5种不同答案(多项选择)的问题。
用户可以通过question_form.html.erb创建一个问题,然后是可能的答案,但它只显示一个框,而不是五个。
question_form.html.erb
<%= form_for([ @poll, @question ]) do |f| %>
<% if @question.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@question.errors.count, "error") %> prohibited this question from being saved:</h2>
<ul>
<% @question.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :kind %><br>
<% @kind_options.each do |option| %>
<label>
<%= f.radio_button :kind, option[1] %>
<%= option[0] %>
</label>
<% end %>
<p>Specify some choices:</p>
<%= f.fields_for :possible_answers do |c| %>
<p>
<%= c.text_field :title, placeholder: "Type your choice", class: "form-control" %>
</p>
<% end %>
</div>
在我的问题控制器中我添加了第5行{@ question.possible_answers.build} 但是当我运行该程序时,它给了我一个盒子来输入答案而不是5?
<div class="actions">
<%= f.submit 'Save', class: "btn btn-primary" %>
</div>
<% end %>
class QuestionsController < ApplicationController
before_action :set_question, only: [:show, :edit, :update, :destroy]
before_action :set_poll
before_action :set_kind_questions
# GET /questions
# GET /questions.json
def index
@questions = Question.all
end
# GET /questions/1
# GET /questions/1.json
def show
end
**# GET /questions/new
def new
@question = @poll.questions.build
5.times { @question.possible_answers.build }
end**
# GET /questions/1/edit
def edit
end
# POST /questions
# POST /questions.json
def create
@question = @poll.questions.build(question_params)
respond_to do |format|
if @question.save
format.html { redirect_to @poll, notice: 'Question was successfully created.' }
format.json { render :show, status: :created, location: @question }
else
format.html { render :new }
format.json { render json: @question.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /questions/1
# PATCH/PUT /questions/1.json
def update
respond_to do |format|
if @question.update(question_params)
format.html { redirect_to @question, notice: 'Question was successfully updated.' }
format.json { render :show, status: :ok, location: @question }
else
format.html { render :edit }
format.json { render json: @question.errors, status: :unprocessable_entity }
end
end
end
# DELETE /questions/1
# DELETE /questions/1.json
def destroy
@question.destroy
respond_to do |format|
format.html { redirect_to questions_url, notice: 'Question was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_question
@question = Question.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def question_params
params.require(:question).permit(:title, :kind, :poll_id)
end
**def set_kind_questions
@kind_options = [
[ "Open Answer", "open" ],
[ "Multiple Choice", "choice" ],
]
end**
def set_poll
@poll = Poll.find params[:poll_id] #/polls/1/questions
end
end
答案 0 :(得分:1)
如果您正在使用Build a Polling Application with Rails,那么您最有可能使用的问题模型(您没有列出)是不完整的,因此Rails会在possible_answers
上窒息在控制器Questions#new
迭代构建方法之前。您需要将accepts_nested_attributes_for :possible_answers
添加到模型中。然后5.times { @question.possible_answers.build }
就可以了。
应用/模型/ question.rb 强>
class Question < ActiveRecord::Base
belongs_to :poll
has_many :possible_answers
accepts_nested_attributes_for :possible_answers
end
我遇到了一些障碍,作者编辑的代码与他的截屏不同步。你必须仔细查看他的啧啧repo上的差异。