我是Rails的新手,如果有人能帮助我,我会很感激。我正在玩这个测验应用程序,我有一个问题和选择控制器和模型。我只有一个课程控制器及其各自的观点。我的问题出于某种原因,无论我创建了多少问题,它们都没有循环,它始终显示ID = 1的问题。在过去的两天里,我一直试图弄清楚这一点,没有运气,任何指示或提示,都欢迎。提前谢谢。
class LessonsController < ApplicationController
def index
end
def start
total = params[:number].to_i
all = Question.where(:all).map {|x| x.id}
session[:questions] = all.sort_by{rand}[0..(total-1)]
session[:total] = total
session[:current] = 0
session[:correct] = 0
redirect_to :action => "question"
end
def question
@current = session[:current]
@total = session[:total]
if @current >= @total
redirect_to :action => "end"
return
end
@question = Question.find_by(session[:questions][@current])
@choices = @question.choices.sort_by{rand}
session[:question] = @question
session[:choices] = @choices
end
def answer
@question = session[:questions]
@current = session[:current]
@total = session[:total]
@choiceid = params[:choice]
@question = session[:question]
@choices = session[:choices]
@choice = @choiceid ? Choice.find(@choiceid) : nil
if @choice and @choice.correct
@correct = true
session[:correct] += 1
else
@correct = false
end
session[:current] += 1
end
def end
@correct = session[:correct]
@total = session[:total]
@score = @correct * 100 / @total
end
end
index.html.erb
<h1>Start Lesson</h1>
<%= form_tag({:controller => "lessons",
:action => "start"}) do %>
<p><%= label_tag(:number, "How many questions do you want in the
lesson?", :id => "question") %></p>
<p class="center"><%= select_tag(:number, options_for_select((1..5).map
{|x| [x, x]}), :id => "count") %> <br /></p>
<div class="center">
<%= submit_tag "Start!", :class => "submit"%>
</div>
<% end %>
<% content_for(:title) do %>
Start Lesson!
<% end %>
应用程序/视图/经验/ question.html.erb
<h1>Question <%= @current + 1 %> of <%= @total %></h1>
<p id="question"><%= @question.text %></p>
<%= form_tag({:controller => "lessons", :action => "answer"}) do %>
<% @choices.each do |choice| %>
<p class="choice">
<%= radio_button_tag(:choice, choice.id) %>
<%= label_tag("choice_".concat(choice.id.to_s).to_sym, choice.text) %>
</p>
<% end %>
<div class="center">
<%= submit_tag("Continue", :class => "submit") %>
</div>
<% end %>
<% content_for(:title) do %>
Question <%= @current + 1 %> of <%= @total %>
<% end %>
answer.html.erb
<h1>Question <%= @current + 1 %> of <%= @total %></h1>
<div id="feedback-choices">
<% @choices.each do |choice| %>
<% if @choice.correct %>
<p class="correct feedback-choice">
<% elsif @choice == @choice %>
<p class="incorrect feedback-choice">
<% else %>
<p class="feedback-choice">
<% end %>
<%= @choice.text %>
</p>
<% end %>
</div>
<p class="feedback">
<% if @correct %>
Correct!
<% else %>
Incorrect.
<% end %>
</p>
<%= form_tag({:controller => "lessons", :action => "question"}) do %>
<div class="center">
<%= submit_tag("Continue", :class => "submit") %>
</div>
<% end %>
<% content_for(:title) do %>
Question <%= @current + 1 %> of <%= @total %>
<% end %>