我創建了一個測驗,並希望顯示在每個頁面隨機看一個隨機...例如,首先它得到隨機數question_id = 2,然後4然後再2等。我不想重複同樣的問題,當我在視圖不想重複在軌道上的紅寶石代碼
這裏點擊旁邊是我的控制器
def answer
@user = current_user
@student = Student.find_by_admission_no(@user.username)
@exam_group = ExamGroup.find(params[:exam_group_id])
@answer = Answer.new
n = Question.count
@ans = random1
end
def ans
@user = current_user
@student = Student.find_by_admission_no(@user.username)
@exam_group = ExamGroup.find(params[:exam_group_id])
end
def create
#@ans = Question.find_by_id(1)
@answer = Answer.new(params[:ans])
@answer.answer = params[:answer]
#@answer.questions_id = @ans.id
if @answer.save
redirect_to :controller => 'answers', :action => 'final'
end
end
def random1
rand_id = (Question.count)
rand_record = Question.first(:conditions => [ "id = ?", rand_id])
end
def random2
if (c = Question.count) != 0
Question.find(:first, :offset =>rand(c))
end
end
當我點擊下一步鑑於
<div class = "main">
<% form_for @answer do |f|%>
<%= render :partial => 'answers/ans' %>
<% end %>
</div>
<div class="extender"></div>
和答案/答案
<div class = "answers">
<div class = "y">
<div class = "label_field_pair">
<label for "questions">
<%= @ans.id %>) <%= @ans.ques %>
</label>
</div> <br>
<div class = "label_field_pair2">
<label for "options">
<div id = "option-1">
<%= radio_button_tag "answer", "#{@ans.id}ans1"%><%= @ans.ans1 %>
</div><br>
<div id = "option-2">
<%= radio_button_tag "answer", "#{@ans.id}ans2"%><%= @ans.ans2 %>
</div><br>
<div id = "option-3">
<%= radio_button_tag "answer", "#{@ans.id}ans3"%><%= @ans.ans3 %>
</div><br>
<div id = "option-4">
<%= radio_button_tag "answer", "#{@ans.id}ans4"%><%= @ans.ans4 %>
</div><br>
</label>
</div>
<%= link_to "Next", :partial =>"ans"%>
</div>
</div>
請幫助我,謝謝
不錯的嘗試,但...沒有。 – zeantsoi
@zeantsoi,你讓我的一天 –