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:

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]
no implicit conversion of Symbol into Integercomes whenever you will be trying to access array element by using string instead of fixnum indices...