Multiple items in a single form - trying to get radio button selected

Here's the setup, fairly standard. I have an order which has multiple order items associated with it. I have a page that displays all the order items associated with the order. There is a single radio button associated with each order item to indicate whether to ship or pickup. Once the user has chosen the selection for each item, the user hits a submit button to save the selection.

Because there a multiple items in the form, how do I get this information in the post? I need a single form as I only want one submit button. Any suggestions?

TIA,

Scott

Hi Scott,

Scott wrote:

I have an order which has multiple order items associated with it. I have a page that displays all the order items associated with the order. There is a single radio button associated with each order item to indicate whether to ship or pickup. Once the user has chosen the selection for each item, the user hits a submit button to save the selection. Because there a multiple items in the form, how do I get this information in the post? I need a single form as I only want one submit button. Any suggestions?

You'll need to dynamically name your radio buttons on render so you can fetch them from the params hash meaningfully. You'll need to use radio_button_tag rather than radio_button to accomplish what you want.

hth, Bill

Thanks Bill. I took your advice and used radio_button_tag and appended the id of the object to the end of the name. Then I parsed out the id on submit.

Cheers,

Scott

Hi Scott,

Scott wrote:

Thanks Bill. I took your advice and used radio_button_tag and appended the id of the object to the end of the name. Then I parsed out the id on submit.

Glad to get you pointed in the right direction. Congrats on working it out!

Best regards, Bill

Sorry, Scott, I have this same problem, could you please write the form as you finally did it?

Thanks a lot.

I mean, if your radio buttons are named dinamically, how then you get them in the controller? if you have <input name="some_name#{id}" type="radio" value="no" /> how do you get then the params[....?]?

Damaris Fuentes wrote:

I mean, if your radio buttons are named dinamically, how then you get them in the controller? if you have <input name="some_name#{id}" type="radio" value="no" /> how do you get then the params[....?]?

if you call the inputs "some_name[#{id}]" then params[:some_name] will
be a hash, the keys will be the ids and the values what the user picked.

Fred

Frederick Cheung wrote:

> > I mean, if your radio buttons are named dinamically, how then you get > them in the controller? > if you have > <input name="some_name#{id}" type="radio" value="no" /> > how do you get then the params[....?]? >

if you call the inputs "some_name[#{id}]" then params[:some_name] will be a hash, the keys will be the ids and the values what the user picked.

There are a couple of things you'll want to get used to doing to investigate this type of question for yourself too. (Please note that I'm not suggesting that it's not ok to ask. I'm only saying "here are a couple of things you can do for yourself that can help you avoid waiting for someone on the list to answer").

First, if you view page source in your browser, you'll see exactly what the Rails helpers did for you. I do note that you're not using the radio_button helper above.

Second, to find out exactly what your params hash contains when it gets to your controller you can: a) in the view your controller method renders, put this somewhere: <%= params.inspect %>, or (if you're really lazy like me) b) in the controller method itself, put 'fail here' or something else that will cause Rails to throw an error. When it does, the page displaying the error message will also tell you what the params hash contained.

Hope this helps, Bill

Frederick Cheung wrote:

I mean, if your radio buttons are named dinamically, how then you get them in the controller? if you have <input name="some_name#{id}" type="radio" value="no" /> how do you get then the params[....?]?

if you call the inputs "some_name[#{id}]" then params[:some_name] will be a hash, the keys will be the ids and the values what the user picked.

Forgot to say if that's actually in the view then you of course need to be using <%= %> rather than just #{}

Second, to find out exactly what your params hash contains when it gets to your controller you can: a) in the view your controller method renders, put this somewhere: <%= params.inspect %>, or (if you're really lazy like me) b) in the controller method itself, put 'fail here' or something else that will cause Rails to throw an error. When it does, the page displaying the error message will also tell you what the params hash contained.

even lazier - just look at development.log

Fred

Hi Fred,

Frederick Cheung wrote:

even lazier - just look at development.log

LOL! I'm unaccustomed to being one-upped on the laziness front :wink:

In all seriousness though, that's excellent advice. There's so much useful info in the logs that I often wonder why I get such 'glazed eye' responses when I render the same. Is it that the logs in other environs are not as useful as they are in Rails?

Best regards and happy holiday! Bill