How do I select multiple things in a form?

At the moment I have the following code:

<% form_for :query, :url => {:action => ‘plot’} do |form| %>

<p>
    <label for "query_product_name"> Product: </label>
    <%= form.select :product_name,
                    @products,
                    :prompt => "Select Product"
    %>
</p>

<%=submit_tag "Display Results", :class => "submit"%>

<%end%>

This enables me to select a single product from a drop down menu of
products.
It then saves this product in query object.

I want to be able to select multiple products and for them to be held as
an array in query.

Is this possible??

Chris.

Chris F. wrote:

    %>

I want to be able to select multiple products and for them to be held as
an array in query.

Is this possible??

Chris.


Posted via http://www.ruby-forum.com/.

Add ‘:multiple=>true’ to the options hash for the select

_Kevin

o.k. so I added ‘:multiple=>true’ in the following way:

<% form_for :query, :url => {:action => ‘plot’} do |form| %>

<p>
    <label for "query_product_name"> Product: </label>
    <%= form.select :product_name,
                    @products,
                    :prompt => "Select Product",
                    :multiple => true
    %>
</p>

<%=submit_tag "Display Results", :class => "submit"%>

<%end%>

but it is still just a combo box that only lets me select one item.
Any other ideas?

Chris

that’s worked - Brilliant.

Thanks very much

I haven’t got it to work quite yet.

When I select multiple products, I was hoping they would be sent as an
array to the query object.

But:

When I select multiple products, only 1 product (the first one) gets
sent.

Do you know how to send them as an array?

Chris

Chris F. wrote:

Do you know how to send them as an array?

Chris


Posted via http://www.ruby-forum.com/.

Give the control a name like :name=>‘products[]’
put that in the second hash with the :multiple=>true

_Kevin

try doing this…

form.select :product_name, @products, {:prompt=>‘select product’},
{:multiple=>true}

_Kevin

Nothing seems to work - could you be more specific?

Chris.

Chris, here is something I have:

FORM:

Categories
<%= options_from_collection_for_select(@all_categories, :id, :long_name, @selected) %>

CONTROLLER:
def create
@photo = Photo.new(params[:photo])
@photo.categories = Category.find(params[:categories]) if
params[:categories]
if @photo.save
flash[:notice] = ‘Photo was successfully created.’
redirect_to :action => ‘list’
else
@all_categories = Category.find(:all, :order => ‘name’)
render :action => ‘new’
end
end

Hope that helps some.

I’m sorry but I’m still puzzled!

I just can’t get the form when submitted to be able to hold multiple
products.

<% form_for :query, :url => {:action => ‘plot’} do |form| %>

<p>
    <label for "query_product_name"> Product: </label>
    <%= form.select :product_name,
                    @products,
                    {:prompt => "Select Product"},
                    {:multiple => true}
    %>
</p>

What am I doing wrong?