# Newbie - question on radio\_buttons

**URL:** <https://www.ruby-forum.com/t/newbie-question-on-radio-buttons/64071>\
**Category:** Rails\
**Created:** [June 21, 2006, 8:44pm UTC](https://www.ruby-forum.com/t/newbie-question-on-radio-buttons/64071 "2006-06-21T20:44:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![seamus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@seamus](https://www.ruby-forum.com/u/seamus)\
**Post date:** [June 21, 2006, 8:44pm UTC](https://www.ruby-forum.com/t/newbie-question-on-radio-buttons/64071/1 "2006-06-21T20:44:55Z")

</div>

I have a view with a form and i want to be able to process the button  
selected in a controller - not sure how to do this. The form does not  
map directly to a model - all I want to do is be able to pass back the  
selected button and know which one it is.

So, i have a form and several radio buttons - the submit (not shown)  
takes me to the process\_answer action where I want to determine which  
button was pressed

\<%= start\_form\_tag(:action =\> “process\_answer”) %\>  
…  
\<%= radio\_button “radio”, “answer”, “a” %\> (A)

inside the process\_answer, I have the following:

def process\_answer  
if params[:answer] == “c”  
redirect\_to(:action =\> “correct\_answer”)  
end

end

it does not like this

any thoughts??

Seamus

---

<div class="post-metadata">

**Author:** ![seamus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@seamus](https://www.ruby-forum.com/u/seamus)\
**Post date:** [June 21, 2006, 10:16pm UTC](https://www.ruby-forum.com/t/newbie-question-on-radio-buttons/64071/2 "2006-06-21T22:16:09Z")

</div>

Also a newbie here, but I believe your data is in params[:radio] not  
params[:answer].

So, try:

radio = params[:radio]  
if radio.answer == “c”

or something like…

if params[:radio].answer == “c”

yb

Seamus Gilchrist wrote:

> I have a view with a form and i want to be able to process the button  
> selected in a controller - not sure how to do this. The form does not  
> map directly to a model - all I want to do is be able to pass back the  
> selected button and know which one it is.
> 
> So, i have a form and several radio buttons - the submit (not shown)  
> takes me to the process\_answer action where I want to determine which  
> button was pressed
> 
> \<%= start\_form\_tag(:action =\> “process\_answer”) %\>  
> …  
> \<%= radio\_button “radio”, “answer”, “a” %\> (A)
> 
> inside the process\_answer, I have the following:
> 
> def process\_answer  
> if params[:answer] == “c”  
> redirect\_to(:action =\> “correct\_answer”)  
> end
> 
> end
> 
> it does not like this
> 
> any thoughts??
> 
> Seamus

---

<div class="post-metadata">

**Author:** ![seamus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@seamus](https://www.ruby-forum.com/u/seamus)\
**Post date:** [June 21, 2006, 10:31pm UTC](https://www.ruby-forum.com/t/newbie-question-on-radio-buttons/64071/3 "2006-06-21T22:31:00Z")

</div>

On Wednesday, June 21, 2006, at 8:44 PM, Seamus Gilchrist wrote:

> …
> 
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

If the buttons don’t correspond to a model attribute, you want to do  
this…

\<%= radio\_button\_tag ‘name’, ‘option 1’ %\> Option 1  
\<%= radio\_button\_tag ‘name’, ‘option 2’ %\> Option 2

Then look for the value in params[:name]

\_Kevin

---

<div class="post-metadata">

**Author:** ![seamus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@seamus](https://www.ruby-forum.com/u/seamus)\
**Post date:** [June 23, 2006, 3:30pm UTC](https://www.ruby-forum.com/t/newbie-question-on-radio-buttons/64071/4 "2006-06-23T15:30:11Z")

</div>

as the params[:radio] is an array of values(according to how many radio  
buttons you put in the form) try params[:radio][:answer]…

if you had a form like this (taken from \_kevin)

\<%= radio\_button\_tag ‘name’, ‘option 1’ %\> Option 1  
\<%= radio\_button\_tag ‘name’, ‘option 2’ %\> Option 2

you could take the first value from params[:name][:option1] and the  
second value from params[:name][:option2] etc

hope it helps,  
s

---

<div class="post-metadata">

**Author:** ![seamus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@seamus](https://www.ruby-forum.com/u/seamus)\
**Post date:** [June 23, 2006, 1:06pm UTC](https://www.ruby-forum.com/t/newbie-question-on-radio-buttons/64071/5 "2006-06-23T13:06:40Z")

</div>

Kevin O. wrote:

> On Wednesday, June 21, 2006, at 8:44 PM, Seamus Gilchrist wrote:
> 
> > …
> > 
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> If the buttons don’t correspond to a model attribute, you want to do  
> this…
> 
> \<%= radio\_button\_tag ‘name’, ‘option 1’ %\> Option 1  
> \<%= radio\_button\_tag ‘name’, ‘option 2’ %\> Option 2
> 
> Then look for the value in params[:name]
> 
> \_Kevin

Kevin,

Thanks, this works.

However, I am still confused about the comments posted up top about  
using something like

if params[:radio].answer == “c”

this is what i was previously trying to do, and i don’t understand why  
it doesn’t work - it would seem like it should somehow because the param  
values are stored in the hash and should be able to be pulled out of it.

assuming that i did a radio button

\<%= radio\_button “radio”, “answer”, “a” %\> (A)

if I do a render(:text =\> params[:radio] I get answer a

so question is why can’t I access (or how do I access) just the a part  
of the params[:radio]

thanks again

---

<div class="post-metadata">

**Author:** ![seamus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@seamus](https://www.ruby-forum.com/u/seamus)\
**Post date:** [June 23, 2006, 6:24pm UTC](https://www.ruby-forum.com/t/newbie-question-on-radio-buttons/64071/6 "2006-06-23T18:24:13Z")

</div>

shai wrote:

> as the params[:radio] is an array of values(according to how many radio  
> buttons you put in the form) try params[:radio][:answer]…
> 
> if you had a form like this (taken from \_kevin)
> 
> \<%= radio\_button\_tag ‘name’, ‘option 1’ %\> Option 1  
> \<%= radio\_button\_tag ‘name’, ‘option 2’ %\> Option 2
> 
> you could take the first value from params[:name][:option1] and the  
> second value from params[:name][:option2] etc
> 
> hope it helps,  
> s

thanks
