Jonathan del Strother
2006-Aug-07 15:30 UTC
[Rails] Getting an array of ids from form checkboxes?
Is it possible to set up a list of checkboxes so that the form parameters are given in an array (eg {"delete_list" => [3,6,7]}) ? At the moment I''m using <%= check_box_tag "delete_list[#{post.id}]" %> ...which gives parameters looking like {"delete_list" => {"3" => "1", "6" => "1", "7" => 1}}, which seems unnecessarily ugly... Any suggestions? Jon
dblack@wobblini.net
2006-Aug-07 15:44 UTC
[Rails] Getting an array of ids from form checkboxes?
Hi -- On Mon, 7 Aug 2006, Jonathan del Strother wrote:> Is it possible to set up a list of checkboxes so that the form parameters are > given in an array (eg {"delete_list" => [3,6,7]}) ? > > At the moment I''m using > <%= check_box_tag "delete_list[#{post.id}]" %> > > ...which gives parameters looking like {"delete_list" => {"3" => "1", "6" => > "1", "7" => 1}}, which seems unnecessarily ugly... > > Any suggestions?Try this (tweaked as needed): <% @posts.each do |post| %> <%= check_box_tag "delete_list[]", post.id %> <% end %> You should end up with params[:delete_list] being an array of the boxes you''ve checked. David -- http://www.rubypowerandlight.com => Ruby/Rails training & consultancy ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <----- http://dablog.rubypal.com => D[avid ]A[. ]B[lack''s][ Web]log http://www.manning.com/black => book, Ruby for Rails http://www.rubycentral.org => Ruby Central, Inc.
Jonathan del Strother
2006-Aug-07 16:42 UTC
[Rails] Getting an array of ids from form checkboxes?
On 7 Aug 2006, at 16:42, dblack@wobblini.net wrote:> Hi -- > > On Mon, 7 Aug 2006, Jonathan del Strother wrote: > >> Is it possible to set up a list of checkboxes so that the form >> parameters are given in an array (eg {"delete_list" => [3,6,7]}) ? >> >> At the moment I''m using >> <%= check_box_tag "delete_list[#{post.id}]" %> >> >> ...which gives parameters looking like {"delete_list" => {"3" => >> "1", "6" => "1", "7" => 1}}, which seems unnecessarily ugly... >> >> Any suggestions? > > Try this (tweaked as needed): > > <% @posts.each do |post| %> > <%= check_box_tag "delete_list[]", post.id %> > <% end %> > > You should end up with params[:delete_list] being an array of the > boxes you''ve checked.Perfect, thanks