views:

31

answers:

1

I'm running Sinatra 1.0 with HAML, my form has a number of checkboxes, for example books I like, and you would select all the books you want. The checkbox name is "books".

In sinatra params['books'] there should be an array of all the books that were checked, but it only has the last item that was checked, not an array.

How can I get all the checked items?

HAML:

%form{:action => "/test", :method => 'post'}
  %input{:name=>'check',:type=>'checkbox',:value=>'item1'} item 1
  %input{:name=>'check',:type=>'checkbox',:value=>'item2'} item 2
  %input{:name=>'check',:type=>'checkbox',:value=>'item3'} item 3
  %input{:type => "submit", :value => "send", :class => "button"}

Sinatra get method

post '/test' do
  puts params['check'] #should be an array but is last item checked
end
A: 

Wouldn't that output a bunch of checkboxes with the same name? If so, params['check'] is probably getting replaced with each new checkbox.

Try naming each one something different. If you really want it in an array, try hacking the names:

%input{:name=>'check[1]',:type=>'checkbox',:value=>'item1'} item 1
%input{:name=>'check[2]',:type=>'checkbox',:value=>'item2'} item 2
%input{:name=>'check[3]',:type=>'checkbox',:value=>'item3'} item 3
Jarrod