# Check BOX problem

**URL:** <https://www.ruby-forum.com/t/check-box-problem/112434>\
**Category:** Rails\
**Created:** [August 18, 2007, 9:39pm UTC](https://www.ruby-forum.com/t/check-box-problem/112434 "2007-08-18T21:39:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [August 18, 2007, 9:39pm UTC](https://www.ruby-forum.com/t/check-box-problem/112434/1 "2007-08-18T21:39:32Z")

</div>

Hi,

I am trying to make page with a list of users, with a check box next to  
each of the user names.  
The list of users is given by an array of users : @users

For eg: if @users has {user1, user2,user3} , my page should look like  
below.

user1 []  
user2 []  
user3 []

How can i achieve this? I went thru many howto’s esp to get confused and  
depressed. Please help.

I tried using this:

check\_box(“user”, user.id, :checked =\> ‘checked’ )

\<% end %\>

\<%= pagination\_links(@user\_pages) %\>

check\_box(“user”, user.id, :checked =\> ‘checked’ ) in my rhtml. But it  
gets printed directly ☹

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [August 18, 2007, 9:53pm UTC](https://www.ruby-forum.com/t/check-box-problem/112434/2 "2007-08-18T21:53:58Z")

</div>

Use “[]”'s to send it in the params as a hash like this:

\<% @users.each do |user| -%\>  
\<%= check\_box(:user, :id, :name =\> “user[#{user.id}]” -%\>  
\<% end -%\>

When this is sent back to the controller, you will have params[:user]  
as a hash with user id’s as the keys and 1 as the value.

Sandeep G. wrote:

> user2 []  
> check\_box(“user”, user.id, :checked =\> ‘checked’ )

–  
Sincerely,

William P.

[http://www.billpratt.net](http://www.billpratt.net)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [August 18, 2007, 10:14pm UTC](https://www.ruby-forum.com/t/check-box-problem/112434/3 "2007-08-18T22:14:58Z")

</div>

2 things I forgot to mention. First, the method passed to check\_box (:id  
in the below example) is evaluated as an integer and any non-zero value  
will check the box. Second, if you use an instance variable named the  
same and the name of the checkbox, you can clean things up a bit like  
this

\<% for @user in @users -%\>  
\<%= check\_box “user[]”, :access\_allowed? -%\>  
\<% end -%\>

This will achieve the same thing. This example assumes there is a method  
for users that denotes whether they have access or not (and whether to  
check the box or not). I’m just playing off your example.

William P. wrote:

> > user2 []  
> > check\_box(“user”, user.id, :checked =\> ‘checked’ )

–  
Sincerely,

William P.

[http://www.billpratt.net](http://www.billpratt.net)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [August 19, 2007, 9:21am UTC](https://www.ruby-forum.com/t/check-box-problem/112434/4 "2007-08-19T09:21:12Z")

</div>

Thanks a Lot! This worked Great!
