# Determining checked checkboxes

**URL:** <https://www.ruby-forum.com/t/determining-checked-checkboxes/103106>\
**Category:** Rails\
**Created:** [May 1, 2007, 10:41pm UTC](https://www.ruby-forum.com/t/determining-checked-checkboxes/103106 "2007-05-01T22:41:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![georgeuoa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7ab992/32.png) [@georgeuoa](https://www.ruby-forum.com/u/georgeuoa)\
**Post date:** [May 1, 2007, 10:41pm UTC](https://www.ruby-forum.com/t/determining-checked-checkboxes/103106/1 "2007-05-01T22:41:27Z")

</div>

Hi guys  
Am a newbie in rails please forgive my ignorance.  
i have set of search results

i have appended a checkbox to each of the results and would like to give  
the user some freedom to choose what he/she likes and manipulate or  
explore them further.

my problem is how to determine which checkboxes are checked and return  
an array of their names, which the server can process.

how do you achieve that in rails?

Thank you.

---

<div class="post-metadata">

**Author:** ![georgeuoa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7ab992/32.png) [@georgeuoa](https://www.ruby-forum.com/u/georgeuoa)\
**Post date:** [May 2, 2007, 4:28pm UTC](https://www.ruby-forum.com/t/determining-checked-checkboxes/103106/2 "2007-05-02T16:28:29Z")

</div>

If I understand you correctly, then in your controller you would like an  
array of the checked items which you will access from params.

I have recently done this for assigning roles to users:

My controller action looked like this:

def assign\_roles  
user\_roles = params[:role]  
user\_roles.each do |job|  
UserRole.create(:user\_id =\> params[:user\_id], :role\_id =\>  
job.to\_i)  
end  
redirect\_to ‘somewhere else’  
end

The params[:role] in this case is an array of checked options from the  
form.  
To see how this is achieved here is the view code:

```
    <% for role in @roles %>
        <tr>
            <td><%= role.role_type %></td>
            <td><%= check_box_tag "role[]", role.id, checked=false

```

%\>  
  
\<% end %\>

As you can see, the name for the check\_box\_tag is an array - role[]. The  
value that is returned is the id of the role (as a string - hence the  
to\_i  
after job in the controller).  
The value is only returned if the checkbox is checked so you get an  
array of  
all the checked id’s.

Regards  
Ivor

---

<div class="post-metadata">

**Author:** ![georgeuoa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7ab992/32.png) [@georgeuoa](https://www.ruby-forum.com/u/georgeuoa)\
**Post date:** [May 2, 2007, 9:35pm UTC](https://www.ruby-forum.com/t/determining-checked-checkboxes/103106/3 "2007-05-02T21:35:27Z")

</div>

Ivor P. wrote:

> If I understand you correctly, then in your controller you would like an  
> array of the checked items which you will access from params.
> 
> I have recently done this for assigning roles to users:
> 
> My controller action looked like this:
> 
> def assign\_roles  
> user\_roles = params[:role]  
> user\_roles.each do |job|  
> UserRole.create(:user\_id =\> params[:user\_id], :role\_id =\>  
> job.to\_i)  
> end  
> redirect\_to ‘somewhere else’  
> end
> 
> The value is only returned if the checkbox is checked so you get an  
> array of  
> all the checked id’s.

Thanks Ivor,

i got the method right but am wondering what is happening when i do  
this:

My view has this  
\<%form\_tag :action=\>‘selected’ do%\>  
\<%for result in @results%\>

\<check\_box\_tag “result[]”,result.id,checked=false%\>  
\<%end%\>  
\<%end%\>

And the in the controller i have this method

def selected  
@selected\_items=params[:result]  
end

The selected.rhtml  
\<%=@selected%\>

however i get a blank page. but when i inspect the logs i see that the  
parameters were posted.

how do i get the checked ids rendered or displayed to the user?

Thanks
