# Multiple selectbox and params

**URL:** <https://www.ruby-forum.com/t/multiple-selectbox-and-params/48697>\
**Category:** Rails\
**Created:** [November 23, 2005, 4:02pm UTC](https://www.ruby-forum.com/t/multiple-selectbox-and-params/48697 "2005-11-23T16:02:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gitte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b19c9b/32.png) [@gitte](https://www.ruby-forum.com/u/gitte)\
**Post date:** [November 23, 2005, 4:02pm UTC](https://www.ruby-forum.com/t/multiple-selectbox-and-params/48697/1 "2005-11-23T16:02:13Z")

</div>

Hi there,

I’m having some trouble figuring this one out:  
I have a multiple select box. Selecting one option, the value gets  
submitted just fine. Selecting two or more options, it’s only the last  
selected that get’s submitted.  
Why?

Greetings,  
Gitte W.

---

<div class="post-metadata">

**Author:** ![gitte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b19c9b/32.png) [@gitte](https://www.ruby-forum.com/u/gitte)\
**Post date:** [November 23, 2005, 4:18pm UTC](https://www.ruby-forum.com/t/multiple-selectbox-and-params/48697/2 "2005-11-23T16:18:22Z")

</div>

How are you using the select box? Are you tying it right to the model  
class?  
If so this isn’t going to work (that I know of).  
Your tag should look something like:

.. Of course with all the other stuff needed as well. You'll notice the name is basically an array name which will allow you to pass as many values as you want for this option and can get it by doing something like params[:my\_options][0].

Hope this helps!  
-Nick

---

<div class="post-metadata">

**Author:** ![gitte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b19c9b/32.png) [@gitte](https://www.ruby-forum.com/u/gitte)\
**Post date:** [November 23, 2005, 5:15pm UTC](https://www.ruby-forum.com/t/multiple-selectbox-and-params/48697/3 "2005-11-23T17:15:42Z")

</div>

I had a similar problem yesterday, even the same perhaps?

My guess is that it is in fact submitting all the values as an array but  
you are not seeing it selected in the form because all the values in the  
array are strings and not numbers.

What I did was before using the array in “selected\_value” I turned all  
the elements in the array to integers using to\_i

Hope that helps you out.

---

<div class="post-metadata">

**Author:** ![gitte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b19c9b/32.png) [@gitte](https://www.ruby-forum.com/u/gitte)\
**Post date:** [November 23, 2005, 5:49pm UTC](https://www.ruby-forum.com/t/multiple-selectbox-and-params/48697/4 "2005-11-23T17:49:01Z")

</div>

I’m doing something similar that uses muliple select menus.

Here is the controller

```auto
def new()
  @side_menu = [build_text_box('Instructions:', @required_text)]

  if request.get?()
    @institution = Institution.new()
  elsif request.post?()
    if params[:commit].eql?('Cancel')
      redirect_to(:action=>'list')
      return nil
    else
      @institution = Institution.new(params[:institution])
      @institution.add_license_types_by_id(params[:license_type_ids])
      if @institution.save()
        flash[:notice] = 'Institution was successfully created.'
      	redirect_to :action=>'list'
      end
    end
  end

  @license_types = LicenseType.find(:all);
  @license_type_ids = []
  @institution.license_types.each { |x| @license_type_ids << x.id }
end
```

Here is code from my view

```auto

<% options =
      options_from_collection_for_select(@license_types,
                                        "name", "id", @license_type_ids) 
%>
<%= select_tag("license_type_ids[]", options, {:multiple=>true, 
:size=>3}) %>

```

Hope this helps.

-Steven

---

<div class="post-metadata">

**Author:** ![gitte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b19c9b/32.png) [@gitte](https://www.ruby-forum.com/u/gitte)\
**Post date:** [November 24, 2005, 9:50am UTC](https://www.ruby-forum.com/t/multiple-selectbox-and-params/48697/5 "2005-11-24T09:50:27Z")

</div>

Nick S. wrote:

> Hope this helps!  
> -Nick

Hi Nick,

Yeah it helped a lot - I didn’t know you had to put [] on the name of  
the input tag.

Well I actually have another problem, because I’m using the multiple  
select in a form that’s an AJAX observe\_form. I’m not sure how to pass  
the selected values in the :with parameter.

Greetings,  
Gitte W.

---

<div class="post-metadata">

**Author:** ![gitte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b19c9b/32.png) [@gitte](https://www.ruby-forum.com/u/gitte)\
**Post date:** [November 25, 2005, 2:31am UTC](https://www.ruby-forum.com/t/multiple-selectbox-and-params/48697/6 "2005-11-25T02:31:28Z")

</div>

Glad it helped! Not so sure about the ajax part though. Only have used  
the very basic parts of that as of yet.
