Total newbie, rather clueless, not a developer, but trying to give RoR a
go just for fun.
Have the Agile book and am trying to create a simple app following the
Depot tutorial in same.
It’s a multiple choice type quiz I am doing.
Have 2 tables. First is called questions, analgous to products in the
Depot tutorial. Each row contains a question and 4 possible answers
giving 5 fields plus the id field. Using the tutorial I can do all the
admin stuff, add questions, add users, list my questions and so on. I’m
really pleased.
I have a page for the punter who is to answer the questions in the quiz.
Each question is shown with a group of radio buttons with which they
make their choice. The idea is the question.id and the value for the
selected radio button is stored in my second table, called
response_items, together with the punters name. So in that table I have
fields for question_id, choice and punter as well as the id field.
I would have like to use the Rails form helper radio_button because, as
I understand it, on submit the value of the choice selected would
automatically be saved in the field. But as I have maybe 10 questions
per page the attribute in each group of radio buttons would have the
same name, reflecting the field to store the values, and my radio
buttons would not really work, the punter could only select one choice
on the whole page.
So rather than having lines like <%= radio_button(“response_item”,
“choices”, 1) %<%= h(question.option_one) %>
I have instead
<%= radio_button(“response_item”, question.id, 1) %><%=
h(question.option_one) %>
My thinking is that the choices the user makes will be stored somewhere,
in a hash I think is the term, when the punter clicks submit. Indeed I
see things like Parameters: {“commit”=>" SAVE ",
“response_item”=>{“1”=>“3”, “2”=>“4”, “3”=>“2”}} when I click submit and
my submit fails. So I am thinking I should be able to iterate through
that and save those values in my second table.
So I have a quiz view that reads :
<%= form_tag :action=>‘save_question’ %>
<% for question in @questions %>
<%= question.heading %>
<%= radio_button("response_item", question.id, 1) %><%=
h(question.option_one) %>
<%= radio_button(“response_item”, question.id, 2) %><%=
h(question.option_two) %>
<%= radio_button(“response_item”, question.id, 3) %><%=
h(question.option_three) %>
<%= radio_button(“response_item”, question.id, 4) %><%=
h(question.option_four) %>
<% end %>
<%= submit_tag(" SAVE ")%>
<%= end_form_tag %>
and in quiz_controller I feel sure I must define save_question which
will pick up those values and save them in response_items. But how?
I would be embarrassed to say for how long I have been puzzling over
this. Maybe I am on the wrong track and should do my radio buttons
different?
The problem is, how to save the punters responses? Any insights would be
very much appreciated. I am a total novice and just doing this for fun
so please don’t beat me up over my ignorance on things RoR.
Many thanks
Graham