# Cannot figure out how to handle multi checkbox

**URL:** <https://www.ruby-forum.com/t/cannot-figure-out-how-to-handle-multi-checkbox/143847>\
**Category:** Rails\
**Created:** [July 23, 2008, 8:13am UTC](https://www.ruby-forum.com/t/cannot-figure-out-how-to-handle-multi-checkbox/143847 "2008-07-23T08:13:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![naijaguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9e8a1a/32.png) [@naijaguy](https://www.ruby-forum.com/u/naijaguy)\
**Post date:** [July 23, 2008, 8:13am UTC](https://www.ruby-forum.com/t/cannot-figure-out-how-to-handle-multi-checkbox/143847/1 "2008-07-23T08:13:30Z")

</div>

I have a checkbox that is not tied to a model. Basically, here’s what  
the beginning looks like in the view:

\<% for entrant in @entrants %\>  
\<tr class="\<%= cycle(“even”, “odd”) %\>" \>

\<%= check\_box (“to\_check”, entrant.firstname, {},  
checked = “false”) %\>

This submits to a controller action that has only the following line:

@firstnames = params[:to\_check]

I’m trying to display the results on the next page:

```
  <% for firstname in @firstnames %>
        <br />
        <%=h firstname %>

    <% end %>

```

This seems really simple. Right now it’s displaying every firstname in  
the array whether it was checked or not. I tried another way and it  
only returned the first element in the array (so the names overwrote  
each other). How do I check in the controller action method to see  
which ones were checked? I’m coming from a Java web background and am a  
beginner with Ruby on Rails and have been Googling extensively, but I’m  
not understanding the examples, and the two recent Rails 2 books I have  
(one in PDF form) are confusing me as well.

I tried to figure out how to use check\_box\_tag as well in case that’s  
more appropriate in this case, but I’m just not sure what to do. How  
should I change the form code and the controller so that I can retrieve  
only the firstnames that were checked? The HTML checkboxes should only  
send up the firstnames when checked.

Thanks so much!

---

<div class="post-metadata">

**Author:** ![naijaguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9e8a1a/32.png) [@naijaguy](https://www.ruby-forum.com/u/naijaguy)\
**Post date:** [July 23, 2008, 8:21am UTC](https://www.ruby-forum.com/t/cannot-figure-out-how-to-handle-multi-checkbox/143847/2 "2008-07-23T08:21:40Z")

</div>

take a look at [#17 HABTM Checkboxes - RailsCasts](http://railscasts.com/episodes/17)

On Jul 23, 8:13 am, Naija G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![naijaguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9e8a1a/32.png) [@naijaguy](https://www.ruby-forum.com/u/naijaguy)\
**Post date:** [July 23, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/cannot-figure-out-how-to-handle-multi-checkbox/143847/3 "2008-07-23T15:43:21Z")

</div>

I had seen that, but it’s not clear to me (and some of the people who  
commented on there agree with me). As I said, my checkboxes are not  
tied to a model.

Dejan D. wrote:

> take a look at [#17 HABTM Checkboxes - RailsCasts](http://railscasts.com/episodes/17)
> 
> On Jul 23, 8:13ï¿½am, Naija G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![naijaguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9e8a1a/32.png) [@naijaguy](https://www.ruby-forum.com/u/naijaguy)\
**Post date:** [July 23, 2008, 6:15pm UTC](https://www.ruby-forum.com/t/cannot-figure-out-how-to-handle-multi-checkbox/143847/4 "2008-07-23T18:15:54Z")

</div>

Actually, I wanted the results in a collection or array to iterate over  
in my next view, so this is what worked for that last line in the  
controller:

@item.keywords = selected\_keywords

I was just trying really hard to use the Rails view helpers for a  
checkbox, but if I have to use straight HTML, then by no means am I  
opposed. It just seems like one of those Rails helpers should work for  
me, but I figure it’s because I don’t understand them and when I look at  
the examples in the API docs, they don’t seem to be applicable for my  
case (or I just don’t get them).

Thanks…

Dejan D. wrote:

> As I find somewhere on the net:
> 
> View (as many times as needed):
> 
> ```
> <input id="item_selected_keywords" name="selected_keywords[]"
> type="checkbox" value="exterior" />
> 
> ```
> 
> Controller:
> 
> ```
> selected_keywords = params[:selected_keywords]
> @item.keywords = selected_keywords.join(" ")
> 
> ```
> 
> On Jul 23, 3:43 pm, Naija G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![naijaguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9e8a1a/32.png) [@naijaguy](https://www.ruby-forum.com/u/naijaguy)\
**Post date:** [July 23, 2008, 5:14pm UTC](https://www.ruby-forum.com/t/cannot-figure-out-how-to-handle-multi-checkbox/143847/5 "2008-07-23T17:14:27Z")

</div>

As I find somewhere on the net:

View (as many times as needed):

```
    <input id="item_selected_keywords" name="selected_keywords[]"
    type="checkbox" value="exterior" />

```

Controller:

```
    selected_keywords = params[:selected_keywords]
    @item.keywords = selected_keywords.join(" ")

```

On Jul 23, 3:43Â pm, Naija G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![naijaguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9e8a1a/32.png) [@naijaguy](https://www.ruby-forum.com/u/naijaguy)\
**Post date:** [July 24, 2008, 4:16am UTC](https://www.ruby-forum.com/t/cannot-figure-out-how-to-handle-multi-checkbox/143847/6 "2008-07-24T04:16:40Z")

</div>

You can use the helper check\_box\_tag

See  
[http://api.rubyonrails.com/classes/ActionView/Helpers/FormTagHelper.html#M001704](http://api.rubyonrails.com/classes/ActionView/Helpers/FormTagHelper.html#M001704)

On Wed, Jul 23, 2008 at 11:15 AM, Naija G.
