# More than one check boxes and selecting all the check boxes

**URL:** <https://www.ruby-forum.com/t/more-than-one-check-boxes-and-selecting-all-the-check-boxes/150961>\
**Category:** Rails\
**Created:** [October 15, 2008, 9:48am UTC](https://www.ruby-forum.com/t/more-than-one-check-boxes-and-selecting-all-the-check-boxes/150961 "2008-10-15T09:48:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![indu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/eada6e/32.png) [@indu](https://www.ruby-forum.com/u/indu)\
**Post date:** [October 15, 2008, 9:48am UTC](https://www.ruby-forum.com/t/more-than-one-check-boxes-and-selecting-all-the-check-boxes/150961/1 "2008-10-15T09:48:59Z")

</div>

Hai  
Just started to learn Ruby on Rails  
I have a list of users displayed as rows and towards each row there is a  
check box. There is a “Select All” , and “Approve” buttons. On clicking  
the Select All button I want to check all the check boxes or can check  
the boxes separtely if don’t want to check all the boxes and then after  
submitting the Approve button wish to change the status of the  
selecteds  
users(which users whose check box is checked) to approved. Pls give me  
suggestions in doing this.

```
<th> <%= submit_tag "Select All", :onclick=>"return check_all()" %>

```

 \<% count = 0 %\> \<%= form\_tag :controller=\>"user",:action=\>"list\_users"%\> \<% for user in @users %\> \<% count = count + 1%\> \<% end %\> \<%= hidden\_field\_tag "h\_id" ,count %\>

| 
### User list
 |
| Name | User Name | Address | Country | Language |
| --- | --- | --- | --- | --- |
| \<%=link\_to user.name,:controller=\>"User",:action=\>"edit", :id=\>user.id %\> | \<%= user.username %\> | \<%= user.address %\> | \<%= user.country.name %\> | \<%= user.language.language %\> | \<%= check\_box\_tag "user\_status"+count.to\_s,user.id%\> |
| \<%= will\_paginate @users %\>  
\<%= submit\_tag "Approve", :controller=\>"user" ,:action=\>"list\_users",:class=\>"test"%\> |

and in my controller I tried this one

def list\_users

if !(params[:search].blank? and params[:country\_id].blank?)  
if request.post? and params[:commit] == “Search”  
@users =  
User.search(params[:search],params[:country\_id],params[:page])  
else

```
@users = User.paginate :page=>params[:page],:per_page => 2

```

end  
else  
@users = User.paginate :page=\>params[:page],:per\_page =\> 2  
end  
if @users.blank?  
flash[:notice] = ‘No Records Found.’  
end

```
if request.post? and params[:commit] == "Approve"
     params[:user_status].each do |ln|
         @user = User.find(ln)
           @user.update_attribute(:user_status,1)
     end

end

```

end  
Now if selected users separately(not by using the “Select All”  
button) the status of the selected users are changing. But the “Select  
All” button is not working. ie when clicking on the “Select All” button  
only the first one in the list is selecting

and in my javascript i wrote  
function check\_all() {  
var count = document.getElementById(‘h\_id’).value;  
for(i=1;i\<=count;i++){  
document.getElementById(‘user\_status’+i).checked = true  
}  
return false;  
}  
Pleas help in solving this problem  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![indu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/eada6e/32.png) [@indu](https://www.ruby-forum.com/u/indu)\
**Post date:** [October 15, 2008, 10:08am UTC](https://www.ruby-forum.com/t/more-than-one-check-boxes-and-selecting-all-the-check-boxes/150961/2 "2008-10-15T10:08:29Z")

</div>

hi u can try this way

in ur view the check\_box likee

 Select All 

and in the user loop

and in ur controller…

for user in params[:user\_status]  
puts user  
###’ should be able get the all the user ids that are selected  
end

hope it will help u.

🙂 Bala  
On Wed, Oct 15, 2008 at 1:18 PM, Indu RS

---

<div class="post-metadata">

**Author:** ![indu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/eada6e/32.png) [@indu](https://www.ruby-forum.com/u/indu)\
**Post date:** [October 15, 2008, 11:09am UTC](https://www.ruby-forum.com/t/more-than-one-check-boxes-and-selecting-all-the-check-boxes/150961/3 "2008-10-15T11:09:18Z")

</div>

Bala kishore Pulicherla wrote:

> for user in params[:user\_status]  
> puts user  
> ###’ should be able get the all the user ids that are selected  
> end
> 
> hope it will help u.
> 
> 🙂 Bala  
> On Wed, Oct 15, 2008 at 1:18 PM, Indu RS  
> Thank you so much for the immediate response and solved my issue
