Radio_button in loop problem

Hi all, I can’t figure out why my radio_buttons won’t reflect the values
stored in my database. I read through some of the previous posts – is
it true that they don’t work with integers?

This is the code I’m using:

<% for question in @survey.questions %>
<%= debug question %>

<%= radio_button ( “question”, :question_id, question.option_one.id)
%>
<%= question.option_one %>

<%= radio_button ( “question”, :question_id, question.option_two.id)
%>
<%= question.option_two %>
<% end %>

The “debug question” statement prints out the correct values. I have to
think that I’m using the wrong parameters in the radio_button call, but
nothing I try is working.

I guess I could use the radio_button_tag builder but radio_button seems
better. Any help would be greatly appreciated!

Thanks,
Eugene

Eugene wrote:

Hi all, I can’t figure out why my radio_buttons won’t reflect the values
stored in my database. I read through some of the previous posts – is
it true that they don’t work with integers?

This is the code I’m using:

<% for question in @survey.questions %>
<%= debug question %>

<%= radio_button ( “question”, :question_id, question.option_one.id)
%>
<%= question.option_one %>

<%= radio_button ( “question”, :question_id, question.option_two.id)
%>
<%= question.option_two %>
<% end %>

The “debug question” statement prints out the correct values. I have to
think that I’m using the wrong parameters in the radio_button call, but
nothing I try is working.

I guess I could use the radio_button_tag builder but radio_button seems
better. Any help would be greatly appreciated!

Thanks,
Eugene

This vaguely rings a bell with me Eugene. It would be helpful if you
could
post the html that this produces. Meantime…

Did you try putting “question_id” rather than :question_id ?

Did you try putting question.option_one.id.to_s ? The helper is maybe
looking for a string, and you are giving it an integer.

Hopefully my lame attempt to help might inspire someone who knows what
they
are talking about to give you a definitive solution :slight_smile:

Robert J.

Hey Robert, thanks for the reply!

This vaguely rings a bell with me Eugene. It would be helpful if you
could
post the html that this produces. Meantime…

Here’s the code I’m using (I remade my project, so some of the names
have changed)

– view code –
<% for choice in @survey.responses %>
<%= debug choice %>
<%= radio_button ( “choice”, “answer_id”,
choice.question.answer_one.id.to_s) %>
<%= choice.question.answer_one.text %>
<%= radio_button ( “choice”, “answer_id”,
choice.question.answer_two.id.to_s) %>
<%= choice.question.answer_two.text %>
<% end %>

– html produced –

--- !ruby/object:Response
attributes:
  question_id: "1"
  survey_id: "1"
  id: "1"
  answer_id: "2"

green
blue

So, debugging the choice var shows that answer_id=2, but the radio input
isn’t selected.

Did you try putting “question_id” rather than :question_id ?

I did try this, and it didn’t change my results.

Did you try putting question.option_one.id.to_s ? The helper is maybe
looking for a string, and you are giving it an integer.

This doesn’t work either, unfortunately.

Hopefully my lame attempt to help might inspire someone who knows what
they
are talking about to give you a definitive solution :slight_smile:

Robert J.

I appreciate your reply, do you (or anyone else) have any other ideas?

Thanks,
Eugene

To all who follow in my footsteps, I’ve decided to go with the
radio_button_tag
(ActionView::Helpers::FormTagHelper)
which was infinitely easier for me to configure. Good luck, and if
anyone does get this working with the radio_button helper, I’d be
interested to know how.

-Eugene

Hmm, I’m starting to think it has something to do with having to use an
instance variable … sound right to you? I’m experimenting right now,
I’ll post any progress.