HELP: multiple select list in ror

Hi

Search doesn’t seem to be working so my apologies if this has come up
before.

I have some simple code in a view that generates a multiple select:

<% for m in Menu.find(:all) %>

" else ">" end %> <%= m.name %>

<% end %>

It generates a nice multiple select and when used to view a menu section
it correctly displays the menus it belongs to even if there are
multiple. However, when saving it only returns one value in the params
even though I select several. My params with 2 menus selected looks like
this:

Parameters: {“commit”=>“Create”, “action”=>“create”,
“controller”=>“menu_sections”, “menu_section”=>{“name”=>“test Menu”,
“display_name”=>"", “position”=>“56”}, “menu_select”=>“1”}

I know there must be something simple wrong but I can’t see what. Why
isn’t the menu_select an array of id’s?

Thanks

ps anybody got a nice acts_as_multiple_selection ?

Hi Donald,

Let’s say that you have a habtm association between menu and user, check
this code snipset:

    <% Menu.find(:all, :order => "name").each_with_index do |k, i| -%>
  • <%= check_box_tag('user[menu_ids][]', k.id, @user.menus.include?(k)) %> <%= k.name %>
  • <% end %>

Jean-Etienne

Thanks Jean-Etienne

Here’s the params with your code:

Parameters: {“commit”=>“Edit”, “action”=>“update”, “id”=>“1”,
“controller”=>“menu_sections”, “menu_section”=>{“name”=>“Appetizers”,
“menu_ids”=>[“2”, “1”], “display_name”=>"", “position”=>“1”},
“menu_select”=>“2”}

So individual check boxes work, but why not a multiple select? From a UI
standpoint it’s a lot easier.

Jean-Etienne D. wrote:

Hi Donald,

Let’s say that you have a habtm association between menu and user, check
this code snipset:

    <% Menu.find(:all, :order => "name").each_with_index do |k, i| -%>
  • <%= check_box_tag('user[menu_ids][]', k.id, @user.menus.include?(k)) %> <%= k.name %>
  • <% end %>

Jean-Etienne

Just figured it out. The

<select name=“menu_select[]” must have the [] other wise it only returns
one.

Jean-Etienne code gave me the hint and also this:

http://www.forthecode.com/user/index/17

Thanks

Donald B. wrote:

Thanks Jean-Etienne

Here’s the params with your code:

Parameters: {“commit”=>“Edit”, “action”=>“update”, “id”=>“1”,
“controller”=>“menu_sections”, “menu_section”=>{“name”=>“Appetizers”,
“menu_ids”=>[“2”, “1”], “display_name”=>“”, “position”=>“1”},
“menu_select”=>“2”}

So individual check boxes work, but why not a multiple select? From a UI
standpoint it’s a lot easier.

Jean-Etienne D. wrote:

Hi Donald,

Let’s say that you have a habtm association between menu and user, check
this code snipset:

    <% Menu.find(:all, :order => "name").each_with_index do |k, i| -%>
  • <%= check_box_tag('user[menu_ids][]', k.id, @user.menus.include?(k)) %> <%= k.name %>
  • <% end %>

Jean-Etienne