In this quiz I'm building when an answer is clicked, I want the @taken instance variable to change from "No" to "Yes"
Here i assign it to "No" if nil in my show action
def show
if @taken == nil
@taken = "No"
end
end
Then here I update it to "Yes" if the submit_answer action is called. Thing is the flash notices are working correctly when an answer is clicked so I know that this action is working. But for some reason it doesn't update the @taken variable. Why?
controller#action
def submit_answer
@answer = Answer.find(params[:answer_id])
if @answer.correct_answer == true
flash[:notice] = "You did it!"
@taken = "Yes"
redirect_to :back
else
flash[:notice] = "Sorry wrong answer!"
@taken = "Yes"
redirect_to :back
end
end
view.html.haml
- @question.answers.each do |answer|
.sub-panel.top-padding.green-hover
- if @taken == "No"
%li.padding-left= link_to "#{answer.content}", submit_answer_path(answer_id: answer.id)
- else
%li.padding-left= answer.content
= @taken.inspect
routes.rb
get 'quizzes/answer/:answer_id', to: 'quizzes#submit_answer', as: :submit_answer
What's happening is it's setting @taken to "No" correctly in the show action. But then after the submit_answer action is called and the flash notice displays, it doesn't update the @taken instance variable to yes.