在我的views/practice_questionx/index.html.erb
文件中,我想在每次页面加载时从数据库中混洗模型的某些属性。我知道我必须将属性放入数组并在它们上调用.shuffle方法,但我真的很难理解语法以及如何做到这一点。
视图/ practice_questions / index.html.erb
<% @practice_questions.each do |practice_question| %>
...
<p class="main-practice-question-answer">
<%= practice_question.answer_choice_one %>
</p>
<p class="main-practice-question-answer">
<%= practice_question.answer_choice_two %>
</p>
<p class="main-practice-question-answer">
<%= practice_question.answer_choice_three %>
</p>
<p class="main-practice-question-answer">
<%= practice_question.answer_choice_four %>
</p>
<p class="main-practice-question-answer">
<%= practice_question.answer_choice_five %>
</p>
控制器/ practice_questions_contoller.rb
class PracticeQuestionsController < ApplicationController
before_action :authenticate_user!
before_action :set_practice_quiz, only: [:show, :edit, :update, :destroy]
before_action :set_practice_question, only: [:show, :edit, :update, :destroy]
@practice_questions = PracticeQuiz.all.friendly.find(params[:practice_quiz_id]).practice_questions.order('created_at ASC').paginate(:page => params[:page], per_page: 1)
# first get the practicequiz with its friendly id, passing it its real id, then get all the pracice questions, then paginate it?
def show
@question = PracticeQuestion.find_by(id: params[:id])
end
private
def set_practice_question
@practice_question = PracticeQuestion.find(params[:id])
end
def set_practice_quiz
@practice_quiz = PracticeQuiz.friendly.find(params[:practice_quiz_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def practice_question_params
params.require(:practice_question).permit(:question, :explanation, :flagged, :answer_choice_one, :answer_choice_two, :answer_choice_three, :answer_choice_four, :answer_choice_five, :answer_choice_correct)
end
end
模式
create_table "practice_questions", force: :cascade do |t|
t.text "question"
t.text "explanation"
t.boolean "flagged"
t.string "user_id"
t.bigint "practice_quiz_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "answer_choice_one"
t.text "answer_choice_two"
t.text "answer_choice_three"
t.text "answer_choice_four"
t.text "answer_choice_five"
t.text "answer_choice_correct"
t.index ["practice_quiz_id"], name: "index_practice_questions_on_practice_quiz_id"
end
最终,我想在每个页面加载时只调整五个答案选项,因此它们每次都以随机顺序显示。我一直想弄清楚几个小时,似乎无法理解如何去做。
答案 0 :(得分:1)
不是最好的解决方案,但可能会节省您的一天。
在你的模特中,
def answers
[
answer_choice_one, answer_choice_two,
answer_choice_three, answer_choice_four,
answer_choice_five
].shuffle
end
在你看来,
<% practice_question.answers.each do |answer| %>
<p class="main-practice-question-answer">
<%= answer %>
</p>
<% end %>
答案 1 :(得分:1)
使用您当前的数据库模式,最简单的方法是将您的答案加载到自己的数组中,对该数组进行洗牌,然后遍历它:
jquery
以下是代码的作用:
<%
answer_choices = %w(one two three four five).map { |number| practice_question.public_send("answer_choice_#{number}") }.shuffle
answer_choices.each do |choice|
%>
<p class="main-practice-question-answer">
<%= choice %>
</p>
<% end %>
对象上调用#public_send来实现此目的,这是调用构建practice_question
,随机输入我们的问题