How do I select multiple things in a form??

Chris Finch wrote:

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.

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

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

_Kevin

try doing this..

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

_Kevin

Chris Finch wrote:

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

--
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

Chris, here is something I have:

FORM:
<p>
  <label for="categories">Categories</label><br />
  <select id="categories" name="categories[]" multiple="multiple"
size="10" style="width: 250px;">
    <%= options_from_collection_for_select(@all_categories,
                         :id, :long_name,
                         @selected) %>
  </select>
</p>

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.