Forum: Ruby on Rails Multiple items in a single form - trying to get radio button

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
125639ddcc0b9119bb84e29416ace63a?d=identicon&s=25 Scott (Guest)
on 2007-03-29 00:32
(Received via mailing list)
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
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2007-03-29 01:48
(Received via mailing list)
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
125639ddcc0b9119bb84e29416ace63a?d=identicon&s=25 Scott (Guest)
on 2007-03-29 07:50
(Received via mailing list)
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
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2007-03-29 14:35
(Received via mailing list)
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
28a4d1b683ec25e1d0312a2a90f63b6f?d=identicon&s=25 Damaris Fuentes (damaris)
on 2008-09-01 14:58
Sorry, Scott, I have this same problem, could you please write the form
as you finally did it?

Thanks a lot.
28a4d1b683ec25e1d0312a2a90f63b6f?d=identicon&s=25 Damaris Fuentes (damaris)
on 2008-09-01 15:03
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:
> Sorry, Scott, I have this same problem, could you please write the form
> as you finally did it?
>
> Thanks a lot.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-09-01 16:32
(Received via mailing list)
On 1 Sep 2008, at 14:03, 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
28a4d1b683ec25e1d0312a2a90f63b6f?d=identicon&s=25 Damaris Fuentes (damaris)
on 2008-09-01 16:42
Ahm, okkk.
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2008-09-01 17:49
(Received via mailing list)
Frederick Cheung wrote:
> 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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-09-01 17:52
(Received via mailing list)
On 1 Sep 2008, at 16:40, Bill Walton wrote:

>>>
>>
>> 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 #{}


>
even lazier - just look at development.log

Fred
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2008-09-02 00:34
(Received via mailing list)
Hi Fred,

Frederick Cheung wrote:
> even lazier - just look at development.log

LOL!  I'm unaccustomed to being one-upped on the laziness front ;-)

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
This topic is locked and can not be replied to.