Rails - Help scoring an online quiz in RoR

Posted by ChrisWesAllen on Stack Overflow See other posts from Stack Overflow or by ChrisWesAllen
Published on 2010-05-26T16:37:48Z Indexed on 2010/05/26 16:41 UTC
Read the original article Hit count: 242

Filed under:
|
|
|

I'm trying to grade a quiz application I would like to make. I have a questions model with and ask(the actual question), 4 choices(a-d), and a correct answer(string).

In the view I have the 4 question being diplayed then the correct answer choice (This is just a test for functionality) and then I created a text_field to accept the users answer choice and a button to refresh the index action which has the scoring logic, for now..

--Do I need to put the text_field within a form_tag?

<p>1. <%= h @question.q1  %></p>
<p>2. <%= h @question.q2  %></p>
<p>3. <%= h @question.q3  %></p>
<p>4. <%= h @question.q4  %></p>
<p>Answer: <%= h @question.correct  %></p>
<%= text_field_tag :choice, params[:choice] %> 
<%= button_to "Grade", {:controller => 'site', :action => "index"}  %> 
<p> <%= @answer %></p>

Heres the index controller action

def index 
      @question = Question.find(1) 
         if @question.correct == params[:choice]
             @answer = 'right'
         else
                @answer = 'wrong'
         end
end

Its not really working. The textfield is supposed to take a letter choice like 'a' or 'c' and compare it with the correct answer in the database.

I would love this to work by radiobuttons, but I'm a newbie to rails so I thought I'd take baby steps.

So if anyone can help me with how to fix this by string, or preferably by radiobuttons, I'd really appreciate it.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about forms