Blog » Blog Entry

Rubyonrails select helper

November 9, 2006

It is possible to use the select helper to produce a working multi option select list. First here is how not to do it:

<%= select 'group',
           'user_id[]',
           @users.collect { |u| [ u.name, u.id ] },
           {},
           { :multiple => true,
             :size     => @users.size } %>

And this isn't even close:

<%= select 'group',
           'user_id',
           @users.collect { |u| [ u.name, u.id ] },
           {},
           { :multiple => true,
             :size     => @users.size,
             :name     => 'group_user_id[]' } %>

Here is the winning code:

<%= select 'group',
           'user_id',
           @users.collect { |u| [ u.name, u.id ] },
           {},
           { :multiple => true,
             :size     => @users.size,
             :name     => 'group[user_id[]]' } >

Yay! This gets you all the select options arriving in an array in params. The thing you have to remember with Rails is you can override anything very easily.

Tags: ruby, rubyonrails, select

« Debian GNU/Linux Kernel Howto Rumsfield leaving office »

Add a comment:

Title:

Comment:

Name:

Email: