# Radio\_button in loop problem

**URL:** <https://www.ruby-forum.com/t/radio-button-in-loop-problem/96744>\
**Category:** Rails\
**Created:** [July 1, 2006, 8:50am UTC](https://www.ruby-forum.com/t/radio-button-in-loop-problem/96744 "2006-07-01T08:50:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![eugene](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8797f3/32.png) [@eugene](https://www.ruby-forum.com/u/eugene)\
**Post date:** [July 1, 2006, 8:50am UTC](https://www.ruby-forum.com/t/radio-button-in-loop-problem/96744/1 "2006-07-01T08:50:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eugene](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8797f3/32.png) [@eugene](https://www.ruby-forum.com/u/eugene)\
**Post date:** [July 1, 2006, 11:08am UTC](https://www.ruby-forum.com/t/radio-button-in-loop-problem/96744/2 "2006-07-01T11:08:37Z")

</div>

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 🙂

Robert J.

---

<div class="post-metadata">

**Author:** ![eugene](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8797f3/32.png) [@eugene](https://www.ruby-forum.com/u/eugene)\
**Post date:** [July 1, 2006, 8:54pm UTC](https://www.ruby-forum.com/t/radio-button-in-loop-problem/96744/3 "2006-07-01T20:54:20Z")

</div>

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 🙂
> 
> Robert J.

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

Thanks,  
Eugene

---

<div class="post-metadata">

**Author:** ![eugene](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8797f3/32.png) [@eugene](https://www.ruby-forum.com/u/eugene)\
**Post date:** [July 2, 2006, 1:48am UTC](https://www.ruby-forum.com/t/radio-button-in-loop-problem/96744/4 "2006-07-02T01:48:00Z")

</div>

To all who follow in my footsteps, I’ve decided to go with the  
radio\_button\_tag  
([ActionView::Helpers::FormTagHelper](http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#M000501))  
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

---

<div class="post-metadata">

**Author:** ![eugene](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8797f3/32.png) [@eugene](https://www.ruby-forum.com/u/eugene)\
**Post date:** [July 1, 2006, 10:58pm UTC](https://www.ruby-forum.com/t/radio-button-in-loop-problem/96744/5 "2006-07-01T22:58:59Z")

</div>

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.
