Newbie - question on radio_buttons

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

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

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

[email protected]
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

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

Kevin O. wrote:

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

[email protected]
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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs