我正在驗證我正在構建的遊戲應用程序的答案。我遇到了正確的數據庫調用問題。驗證答案
問題#validate_answer
def validate_answer
@answer = Answer.where(correct: true)
@correct_answer = @answer
@selected_answer = params[:answer]
#check if the submitted answer is the correct answer
if @selected_answer == @correct_answer
render :success
else
render :error
end
end
Question.rb
class Question < ActiveRecord::Base
belongs_to :category
has_many :answers
has_one :video_clue
has_many :answers
def correct_answer
answers.find_by correct: true
end
end
Answer.haml
%form#form{:action => results_path(@question), :method => "post"}
- @answers.each do |answer|
.form-group
.radio
%input{:name => 'q_id', :type => 'hidden',:value =>'#{@question.id}'}
%input.btn.btn-default.btn-block{:name => 'answer', :style => 'vertical-align: middle; margin:0px;', :type => 'submit', :value => answer.text}
Seeds.rb
q = Question.create question: "In what year did MTV (Music Television) premiere and what was the first music video the channel aired?", category_id: 1
q.answers.create text: '1982 Michael Jackson Bille Jean'
q.answers.create text: '1984 Madonna Like a virgin'
q.answers.create text: '1981 The Buggles Video Killed The Radio Star', correct: true
目標是將所選答案與正確答案進行匹配。它不斷呈現'失敗'頁面。
你能回答得問題?你必須首先在'validate_answer'中確定你的問題。如果你可以得到這個問題,你可以在'def validate_answer'中更改你的'@answer = @ question.correct_answer' – akbarbin
我已經試過並且沒有工作 –