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: <select name="menu_select" size="5" multiple="multiple"> <% for m in Menu.find(:all) %> <!-- this needs to be abstracted in ror fashion --> <option value="<%= m.id %>" <%= if @menu_section.in_menu?(m) then "selected=\"selected\">" else ">" end %> <%= m.name %> </option> <% end %> </select> 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 ? -- Posted via http://www.ruby-forum.com/.
Hi Donald, Let''s say that you have a habtm association between menu and user, check this code snipset: <ul><% Menu.find(:all, :order => "name").each_with_index do |k, i| -%> <li> <%= check_box_tag(''user[menu_ids][]'', k.id, @user.menus.include?(k)) %> <%= k.name %> </li> <% end %></ul> Jean-Etienne -- Posted via http://www.ruby-forum.com/.
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 Durand wrote:> Hi Donald, > > Let''s say that you have a habtm association between menu and user, check > this code snipset: > > <ul><% Menu.find(:all, :order => "name").each_with_index do |k, i| -%> > <li> > <%= check_box_tag(''user[menu_ids][]'', k.id, @user.menus.include?(k)) > %> > <%= k.name %> > </li> > <% end %></ul> > > Jean-Etienne-- Posted via http://www.ruby-forum.com/.
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 Brady 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 Durand wrote: >> Hi Donald, >> >> Let''s say that you have a habtm association between menu and user, check >> this code snipset: >> >> <ul><% Menu.find(:all, :order => "name").each_with_index do |k, i| -%> >> <li> >> <%= check_box_tag(''user[menu_ids][]'', k.id, @user.menus.include?(k)) >> %> >> <%= k.name %> >> </li> >> <% end %></ul> >> >> Jean-Etienne-- Posted via http://www.ruby-forum.com/.