How can I get all the checked items from a submitted form with sinatra's params?

Posted by 102405176597896213397 on Stack Overflow See other posts from Stack Overflow or by 102405176597896213397
Published on 2010-06-15T02:04:41Z Indexed on 2010/06/15 2:12 UTC
Read the original article Hit count: 188

Filed under:
|
|
|

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

© Stack Overflow or respective owner

Related posts about ruby

Related posts about webforms