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 -- Posted via http://www.ruby-forum.com/.
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-- Posted via http://www.ruby-forum.com/.
On Wednesday, June 21, 2006, at 8:44 PM, 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 > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsIf 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 -- Posted with http://DevLists.com. Sign up and save your mailbox.
Kevin Olbrich wrote:> On Wednesday, June 21, 2006, at 8:44 PM, Seamus Gilchrist wrote: >>.... >> >>Rails@lists.rubyonrails.org >>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] > > _KevinKevin, 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 -- Posted via http://www.ruby-forum.com/.
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 -- Posted via http://www.ruby-forum.com/.
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, > sthanks -- Posted via http://www.ruby-forum.com/.