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.
bill walton
2009-Jul-26 19:05 UTC
Re: How to pass the value from radio button to controller?
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. <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 :) On 7월27일, 오전4시05분, bill walton <bwalton...@gmail.com> wrote:> 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. > > <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
bill walton
2009-Jul-26 21:00 UTC
Re: How to pass the value from radio button to controller?
On Sun, 2009-07-26 at 13:06 -0700, serenobs wrote:> Thank you for reply. > It really helped! > > Thanks again :)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.. On 7월27일, 오전6시00분, bill walton <bwalton...@gmail.com> wrote:> On Sun, 2009-07-26 at 13:06 -0700, serenobs wrote: > > Thank you for reply. > > It really helped! > > > Thanks again :) > > You''re welcome. > > Best regards, > Bill
bill walton
2009-Jul-27 22:18 UTC
Re: How to pass the value from radio button to controller?
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 <div id="whatever">...</div> and :submit => ''whatever''. Assuming you name them group1 and group2, you''ll access via params[:group1] and params[:group2]