0

Ruby: 2.0

Rails: 4.0

I am attempting to pull answer choices from my quiz application. Users select the radio button for =what they believe to be the correct answer in this form:

<%= form_tag do |f| %>
    <%= hidden_field_tag :quiz_id, @quiz.id %>
    <% @quiz.questions.each do |question| %>
        <div class="question">
            <p><%= question.text %></p>
            <ul>
                <% question.answers.each do |answer| %>
                    <% if answer.text.length > 0 %>
                        <li>
                            <%= radio_button_tag "[questions][#{question.id}]selected_answer", answer.id %>
                            <%= answer.text %>
                        </li>
                    <% end %>
                <% end %>
            </ul>
        </div>
    <% end %>
    <%= submit_tag "Score this Quiz" %>
<% end %>

These parameters seem to be coming through okay - you can see them in the log using Spike:

Spike Screenshot

However, I cannot figure out how to make a call to these parameters, here is my controller logic:

def score
   @answers = []
   @quiz = Quiz.find(params[:quiz_id])
   @quiz.questions.each do |question|
       @answers << params[:questions[question.id][:selected_answer]]
   end
end

But that throws the error: no implicit conversion of Symbol into Integer

How do I go about grabbing the selected_answer for each question?

Update:

I have made the changes recommended in the comments:

form

<%= radio_button_tag "questions[#{question.id}][selected_answer]", answer.id %>

controller

@quiz.questions.each do |question|
    @answers << params[:questions][question.id][:selected_answer]
end

This is now giving me this error: undefined method[]' for nil:NilClass` On this line:

@answers << params[:questions][question.id][:selected_answer]
2
  • no implicit conversion of Symbol into Integer comes whenever you will be trying to access array element by using string instead of fixnum indices... Commented Sep 2, 2013 at 20:35
  • @Babai I don't think I understand you advice well enough to make a correction. Can you elaborate? Commented Sep 2, 2013 at 20:37

2 Answers 2

3

This just looks like a typo. Should be:

@answers << params[:questions]["#{question.id}"][:selected_answer]

Note the slightly different placement of brackets.

Also, we may need to translate the question.id to a string, since the params hash will have string keys (it's a HashWithIndifferentAccess, so you can also use symbols, but I think not integers).

Sign up to request clarification or add additional context in comments.

4 Comments

Are you sure you've got a value for every question in @quiz.questions? This error most likely means that in your each block one of params[:questions][question.id] is nil for some value of question.id. But it's hard to say for sure without more information.
You might also try doing some string interpolation - I'm not sure if HashWithIndifferentAccess works with int keys or just strings/symbols. That is, maybe you need params[:questions]["#{question.id}"].
Yes. But to double check myself - I checked the logs, saw that 3 was one of the parameters, and updated the line to: @answers << params[:questions][3][:selected_answer] and got the same error.
Nailed it. Thank you!
0

Most likely you are looking for

<%= radio_button_tag "questions[#{question.id}][selected_answer]", answer.id %>

And you your controller

@answers << params[:questions][question.id][:selected_answer]

You can see exactly the content of params in the log. Take a look at questions and you will be able to see the exact structure in ruby code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.