bsirang
bsirang

Reputation: 473

HTTP Post with Multiple Checkboxes (RoR)

I'm attempting to write a RoR controller that will post data to a page on behalf of a preexisting form. This form consists of a long list of checkboxes.

I use a hash that represents the name => value pairs of the form elements. The problem is that all the checkboxes in the form use the same name but different value. I do not know how to represent multiple values for the same name in my form hash.

So as it stands I can only emulate having a single checkbox selected.

How can I represent multiple checkboxes being checked in my form hash for my RoR controller?

Upvotes: 1

Views: 2191

Answers (2)

sissonb
sissonb

Reputation: 3780

This will pass all the checked values as an array.

<input type="checkbox" name="theName[]"/>
<input type="checkbox" name="theName[]"/>

Upvotes: 3

njorden
njorden

Reputation: 2606

It comes through as an array in that case: so the params would include something like {:user => {:activity => ['hiking', 'boating', 'swimming']}}

Check out: http://www.skuunk.com/2008/05/checkbox-arrays-in-rails.html

Upvotes: 1

Related Questions