# Multiple checkboxes

**URL:** <https://www.ruby-forum.com/t/multiple-checkboxes/62450>\
**Category:** Rails\
**Created:** [June 4, 2006, 11:56pm UTC](https://www.ruby-forum.com/t/multiple-checkboxes/62450 "2006-06-04T23:56:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![martinr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/87869e/32.png) [@martinr](https://www.ruby-forum.com/u/martinr)\
**Post date:** [June 4, 2006, 11:56pm UTC](https://www.ruby-forum.com/t/multiple-checkboxes/62450/1 "2006-06-04T23:56:18Z")

</div>

Please could someone suggest what might be wrong here. In my view I’m  
displaying a series of check boxes and when the form is submitted I want  
the controller to iterate over the values and contatenate them all into  
a string “1” if the box was checked and “0” if it wasn’t.  
I’m new to Ruby and Rails and I can’t seem to get any values out of my  
params other than “0”,

Here’s my code:

View:

\<%= form\_tag :action=\>‘save\_question’%\>

### \<%= @question.title %\>
 \<% for option in @options %\> \<%= option.option\_text %\> \<% end %\> \<%= submit\_tag%\> \<%= end\_form\_tag%\>

Controller:

def save\_question

@options = Option.find\_all\_by\_question\_id(session[:question\_id])  
response = “”  
for option in @options do  
if params[:option][option.id]==“1”  
response \<\< “1”  
else  
response \<\< “0”  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![martinr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/87869e/32.png) [@martinr](https://www.ruby-forum.com/u/martinr)\
**Post date:** [June 5, 2006, 3:11am UTC](https://www.ruby-forum.com/t/multiple-checkboxes/62450/2 "2006-06-05T03:11:45Z")

</div>

I did something similar recently. Here is the code from my controller  
that  
pulled out the items that were checked and saved them:

obj\_keys = []  
params[:objective].each\_key {|k| obj\_keys.push( k ) if  
@params[:objective][k].to\_i == 1}  
@course.objectives = Objective.find( obj\_keys )

Aaron

---

<div class="post-metadata">

**Author:** ![martinr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/87869e/32.png) [@martinr](https://www.ruby-forum.com/u/martinr)\
**Post date:** [June 5, 2006, 3:20am UTC](https://www.ruby-forum.com/t/multiple-checkboxes/62450/3 "2006-06-05T03:20:58Z")

</div>

Your problem is probably because of what you set “value” to be.

I think if you exam your params \<%= debug params %\> you will see that  
the  
option is returning the value that you are setting its ‘checked value’  
to.

Secondly, if there were 15 check boxes, they are all named the same  
thing,  
so as far as my understanding goes, each checkbox will overwrite itself.

May I recommend this in your view?

\<%= form\_tag :action=\>‘save\_question’%\>

### \<%= @question.title %\>
 \<% for option in @options %\> \<%= option.option\_text %\> \<% end %\> \<%= submit\_tag%\> \<%= end\_form\_tag%\>

Then your controller can do this…

@options = Option.find\_all\_by\_question\_id(session[:question\_id])  
response = “”  
for option in @options do  
response \<\< params[:options][option.id.to\_s]  
end  
end

Haven’t tested this code, but it should work.

-hampton.

---

<div class="post-metadata">

**Author:** ![martinr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/87869e/32.png) [@martinr](https://www.ruby-forum.com/u/martinr)\
**Post date:** [June 6, 2006, 12:31am UTC](https://www.ruby-forum.com/t/multiple-checkboxes/62450/4 "2006-06-06T00:31:03Z")

</div>

Hampton wrote:

> Your problem is probably because of what you set “value” to be.
> 
> I think if you exam your params \<%= debug params %\> you will see that  
> the  
> option is returning the value that you are setting its ‘checked value’  
> to.
> 
> Secondly, if there were 15 check boxes, they are all named the same  
> thing,  
> so as far as my understanding goes, each checkbox will overwrite itself.
> 
> May I recommend this in your view?
> 
> \<%= form\_tag :action=\>‘save\_question’%\>
> 
> ### \<%= @question.title %\>
> \<% for option in @options %\> \<%= option.option\_text %\> \<% end %\> \<%= submit\_tag%\> \<%= end\_form\_tag%\>
> 
> Then your controller can do this…
> 
> @options = Option.find\_all\_by\_question\_id(session[:question\_id])  
> response = “”  
> for option in @options do  
> response \<\< params[:options][option.id.to\_s]  
> end  
> end
> 
> Haven’t tested this code, but it should work.
> 
> -hampton.

Thanks for that - I’ve got it working now. i took your suggestions, but  
for some reason if a checkbox isn’t checked a value doesn’t get returned  
so if I use the code

> for option in @options do  
> response \<\< params[:options][option.id.to\_s]  
> end

I get a nil object error for unchecked checkboxes.

The actual code I got working is

for option in @options do  
if params[:option][option.id.to\_s]==“1”  
response \<\< params[:option][option.id.to\_s]  
else  
response \<\< “0”  
end  
end

and the view code worked just as you said.

---

<div class="post-metadata">

**Author:** ![martinr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/87869e/32.png) [@martinr](https://www.ruby-forum.com/u/martinr)\
**Post date:** [June 6, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/multiple-checkboxes/62450/5 "2006-06-06T14:20:51Z")

</div>

Actually, I believe the helper works like this.

\<%= option.option\_text %\>

That should return a 0 value if unchecked.

-hampton.
