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,

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.

<div id="radio_buttons">
  <%= radio_button_tag 'exp', 1 %>
  <%= radio_button_tag 'exp', 2 %>
</div>

<%= 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:

You're welcome.

Best regards,
Bill

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 <div id="whatever">...</div> and :submit =>
'whatever'. Assuming you name them group1 and group2, you'll access via
params[:group1] and params[:group2]