How to pass the value from radio button to controller?

Hi.

I used radio_button_tag.
and I want to pass the value of selected radio button to controller.

<%= radio_button_tag ‘exp’, 1 %>
<%= radio_button_tag ‘exp’, 2 %>

<%= link_to :action=>“do” %>
can i pass the radio button’s value to above method as a parameter?

Thanks in advance.

Hi serenobs,

On Sun, 2009-07-26 at 11:35 -0700, serenobs wrote:

Hi.

I used radio_button_tag.
and I want to pass the value of selected radio button to controller.

<%= radio_button_tag ‘exp’, 1 %>
<%= radio_button_tag ‘exp’, 2 %>

<%= link_to :action=>“do” %>
can i pass the radio button’s value to above method as a parameter?

You can using link_to_remote, which generates an Ajax request.

<%= radio_button_tag 'exp', 1 %> <%= radio_button_tag 'exp', 2 %>

<%= link_to_remote ‘Submit now’, :url => {:action=>“do”}, :submit =>
‘radio_buttons’ %>

The selected radio button’s value will be accessable in your controller
method via the name you’ve given your radio buttons. So in this case,
the ‘do’ method in your controller would probably look something like:

def do

if params[:exp] = ‘1’ #remember that params come back as strings
do_something
elsif params[:exp] = ‘2’
do_something_else
else
handle_the_no_default_radio_button_case # if you want one to be
checked, supply true as the third parameter
end

respond_to do |format|
format.js {
render :update do |page|
page.replace_html or some other reponse
end
}
end
end

HTH,
Bill

Thank you for reply.
It really helped!

Thanks again :slight_smile:

ah, one more question.
if there are 2 radio button groups, then how can I handle these
things?

:submit => ‘group1’, ‘group2’ may be not working…

On Sun, 2009-07-26 at 14:27 -0700, serenobs wrote:

ah, one more question.
if there are 2 radio button groups, then how can I handle these
things?

:submit => ‘group1’, ‘group2’ may be not working…

put them both inside one

and :submit =>
‘whatever’. Assuming you name them group1 and group2, you’ll access via
params[:group1] and params[:group2]

On Sun, 2009-07-26 at 13:06 -0700, serenobs wrote:

Thank you for reply.
It really helped!

Thanks again :slight_smile:

You’re welcome.

Best regards,
Bill

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