Mutiple select

Hello:
How do i make a mutiple select list for “nomodel” objects?I made that
by using something like:<%=option
groups collection for select(…)-%>.But when i select mutiple
items and submit them,I can only get one selected item?
Who can help me?

Who can help me?

Guo Y. wrote:

How do i make a mutiple select list for “nomodel” objects?I made that
by using something like:<%=option
groups collection for select(…)-%>.But when i select mutiple
items and submit them,I can only get one selected item?

Add:

:multiple => true

to the options parameter to your select call.

See:
http://api.rubyonrails.com/classes/ActionView/Helpers/FormTagHelper.html#M001037

Thank you for your reminding!

Hello:
I have added the :multiple=>true to the select_tag.But still,only one
selected item was submitted when selected the mutiple items.I don’t know
why?

<%form_remote_tag(:update=>“give”,:url=>{:action=>“gift_given”,:id=>gift})
do-%>
<%=select_tag(“products”,option_groups_from_collection_for_select
(@ng_category_groups,“options”,“name”,“product_id”,“product_name”),:multiple=>true)-%>

<%=submit_tag "give"-%>

<%end-%>

Guo Y. wrote:

I have added the :multiple=>true to the select_tag.But still,only one
selected item was submitted when selected the mutiple items.I don’t know
why?

So you can select multiple items? If so, what are the parameters
recorded in your log file when posting the form? And what is your code
in the controller that receives the form data?

If you can select multiple items, then there should be multiple items in
the parameters, so if this is still not working for you I suspect it is
because of the way you read the values…

who can help me?

Guo Y. wrote:

I can select multiple items using mouse while pushing ctrl key,but can
not select multiple items only using mouse.

This is normal behaviour.

After selecting mutiple items
and submitting, there is only one item submitted in the params.

Having looked at your view again, the name used is “products” which will
create a select returning a single item (even though multiple can be
selected). To return multiple items, the name must end in “[]”, so:

<%=select_tag(“products[]”,option_groups_from_collection_for_select
(@ng_category_groups,“options”,“name”,“product_id”,“product_name”),:multiple=>true)-%>

If this is in a form for a specific object (say “my_object”) and you
want the products returned to be grouped in the params hash for this
object, then use the name “my_object[products][]”.

I can select multiple items using mouse while pushing ctrl key,but can
not select multiple items only using mouse.After selecting mutiple items
and submitting, there is only one item submitted in the params.
Collection:@ng_category_groups is the return value of some method
which i called in the view in the help file.I have not designed the
method to recieve
form data because the recieved params include only one item.

Mark B. wrote:

So you can select multiple items? If so, what are the parameters
recorded in your log file when posting the form? And what is your code
in the controller that receives the form data?

If you can select multiple items, then there should be multiple items in
the parameters, so if this is still not working for you I suspect it is
because of the way you read the values…

Thank you!I think i can manage it now according your advice.

Mark B. wrote:

Having looked at your view again, the name used is “products” which will
create a select returning a single item (even though multiple can be
selected). To return multiple items, the name must end in “[]”, so:

<%=select_tag(“products[]”,option_groups_from_collection_for_select
(@ng_category_groups,“options”,“name”,“product_id”,“product_name”),:multiple=>true)-%>

If this is in a form for a specific object (say “my_object”) and you
want the products returned to be grouped in the params hash for this
object, then use the name “my_object[products][]”.