# check\_box\_tag problems

**URL:** <https://discuss.rubyonrails.org/t/check-box-tag-problems/52211>\
**Category:** rubyonrails-talk\
**Created:** [June 9, 2010, 2:10pm UTC](https://discuss.rubyonrails.org/t/check-box-tag-problems/52211 "2010-06-09T14:10:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [June 9, 2010, 2:10pm UTC](https://discuss.rubyonrails.org/t/check-box-tag-problems/52211/1 "2010-06-09T14:10:03Z")

</div>

Hi, I'm having some trouble with my form. The thing is I'm using a loop to generate check\_box\_tags and then when the user clicks submit I want to be able to see what he checked. For each checked box I want to copy a file to the corresponding directory.

Here's the form:

\<%form\_for :cats, :url =\> {:action =\> :save\_mt} do%\> &nbsp;&nbsp;\<%@flokkar.each do |flokkur|%\> &nbsp;&nbsp;\<p\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= check\_box\_tag("cats[#{flokkur}]",1,false) %\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= flokkur %\> &nbsp;&nbsp;\</p\> &nbsp;&nbsp;\<%end%\> &nbsp;&nbsp;\<%= submit\_tag "Vista mælitæki", :class =\> 'submit'%\> \<%end%\>

Development.log shows these parameters have been passed:

Parameters: {"commit"=\>"Vista mælitæki", "authenticity\_token"=\>"B2u5ULNr7IJ/ta0+hiAMBjmjEtTtc/yMAQQvSxFn2d0=", "cats"=\>{"Geðsjúkraþjálfun"=\>"1"}}

Here I only checked "Geðsjúkraþjálfun" and then submitted.

The problem is in the controller. How can I loop through the cats hash and check if the key is there? I now have something like this:

@flokkar.each do |flokkur| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if params[:cats][:flokkur] == 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;....do some stuff... &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end end

#(@flokkar is the array that I used to build the form.)

But i never execute the block inside the if condition check. I've also tried params[:cats][flokkur] but nothing happens. How can I make ruby check whether a key exists? Like if I had a string "foo" and a hash "durr", how could I check if durr[:foo] exists without writing the whole thing??

BR, Sindri

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [June 9, 2010, 4:08pm UTC](https://discuss.rubyonrails.org/t/check-box-tag-problems/52211/2 "2010-06-09T16:08:49Z")

</div>

Hi,

Are you trying to apply a loop on cats hash??

If yes then try this:

@var1 = params[:cats].each do |key, value| &nbsp;&nbsp;@var1 \<\< key end

if @var1.include?(your\_key) &nbsp;&nbsp;do this else &nbsp;&nbsp;do that end

Regards,

Saurabh

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [June 9, 2010, 7:38pm UTC](https://discuss.rubyonrails.org/t/check-box-tag-problems/52211/3 "2010-06-09T19:38:47Z")

</div>

Sindri Guðmundsson wrote:

> Hi, I'm having some trouble with my form. The thing is I'm using a loop to generate check\_box\_tags and then when the user clicks submit I want to be able to see what he checked. For each checked box I want to copy a file to the corresponding directory.
> 
> Here's the form:
> 
> \<%form\_for :cats, :url =\> {:action =\> :save\_mt} do%\> &nbsp;&nbsp;\<%@flokkar.each do |flokkur|%\> &nbsp;&nbsp;\<p\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= check\_box\_tag("cats[#{flokkur}]",1,false) %\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= flokkur %\> &nbsp;&nbsp;\</p\> &nbsp;&nbsp;\<%end%\> &nbsp;&nbsp;\<%= submit\_tag "Vista mælitæki", :class =\> 'submit'%\> \<%end%\>

[...]

This is strange code. You're inside a form\_for, so why are you using check\_box\_tag rather than check\_box ?

Best,

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [June 9, 2010, 10:21pm UTC](https://discuss.rubyonrails.org/t/check-box-tag-problems/52211/4 "2010-06-09T22:21:33Z")

</div>

Marnen Laibow-Koser wrote:

> [...]
> 
> This is strange code. You're inside a form\_for, so why are you using check\_box\_tag rather than check\_box ?

I read somewhere that I shouldn't do it like this, but I couldn't get the info on checked/uncecked using check\_box. Should I get rid of the form\_for and change the submit button to POST button\_to? Why?

Thanks Saurabh Peshkar, ofc I should've done that, thanks! 🙂

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [June 9, 2010, 10:43pm UTC](https://discuss.rubyonrails.org/t/check-box-tag-problems/52211/5 "2010-06-09T22:43:01Z")

</div>

Sindri Guðmundsson wrote:

> Marnen Laibow-Koser wrote:
> 
> > [...]
> > 
> > This is strange code. You're inside a form\_for, so why are you using check\_box\_tag rather than check\_box ?
> 
> I read somewhere that I shouldn't do it like this, but I couldn't get the info on checked/uncecked using check\_box. Should I get rid of the form\_for and change the submit button to POST button\_to? Why?

No, you should fix the problem that's preventing you from using check\_box!

> Thanks Saurabh Peshkar, ofc I should've done that, thanks! 🙂

Best,

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [June 9, 2010, 11:04pm UTC](https://discuss.rubyonrails.org/t/check-box-tag-problems/52211/6 "2010-06-09T23:04:44Z")

</div>

Marnen Laibow-Koser wrote:

> Sindri Guðmundsson wrote:
> 
> > Marnen Laibow-Koser wrote:
> > 
> > > [...]
> > > 
> > > This is strange code. You're inside a form\_for, so why are you using check\_box\_tag rather than check\_box ?
> > 
> > I read somewhere that I shouldn't do it like this, but I couldn't get the info on checked/uncecked using check\_box. Should I get rid of the form\_for and change the submit button to POST button\_to? Why?
> 
> No, you should fix the problem that's preventing you from using check\_box!
> 
> > Thanks Saurabh Peshkar, ofc I should've done that, thanks! 🙂
> 
> Best, -- Marnen Laibow-Koser [http://www.marnen.org](http://www.marnen.org) marnen@marnen.org

Hi, I've tried that, but I can't seem to have check\_box write anything else to params[:cats] other than nil. The only thing that happened was that params[:cats] was an array the same length as the number of categories I had but only contained nil-objects. Could you please show me how I should generate the check\_boxes inside the loop in my view?

BR, Sindri

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [June 9, 2010, 11:07pm UTC](https://discuss.rubyonrails.org/t/check-box-tag-problems/52211/7 "2010-06-09T23:07:20Z")

</div>

If this helps these are the parameters passed when I use check\_box:

&nbsp;&nbsp;Parameters: {"commit"=\>"Vista mælitæki", "authenticity\_token"=\>"B2u5ULNr7IJ/ta0+hiAMBjmjEtTtc/yMAQQvSxFn2d0=", "cats"=\>{"flokkur"=\>[nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]}}

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [June 10, 2010, 2:33am UTC](https://discuss.rubyonrails.org/t/check-box-tag-problems/52211/8 "2010-06-10T02:33:37Z")

</div>

Sindri Guðmundsson wrote:

> If this helps these are the parameters passed when I use check\_box:
> 
> &nbsp;&nbsp;Parameters: {"commit"=\>"Vista mælitæki", "authenticity\_token"=\>"B2u5ULNr7IJ/ta0+hiAMBjmjEtTtc/yMAQQvSxFn2d0=", "cats"=\>{"flokkur"=\>[nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]}}

And what does that version of the view code look like?

Best,

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [June 10, 2010, 9:34am UTC](https://discuss.rubyonrails.org/t/check-box-tag-problems/52211/9 "2010-06-10T09:34:34Z")

</div>

Marnen Laibow-Koser wrote:

> Sindri Guðmundsson wrote:
> 
> > If this helps these are the parameters passed when I use check\_box:
> > 
> > &nbsp;&nbsp;Parameters: {"commit"=\>"Vista mælitæki", "authenticity\_token"=\>"B2u5ULNr7IJ/ta0+hiAMBjmjEtTtc/yMAQQvSxFn2d0=", "cats"=\>{"flokkur"=\>[nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]}}
> 
> And what does that version of the view code look like?
> 
> Best, -- Marnen Laibow-Koser [http://www.marnen.org](http://www.marnen.org) marnen@marnen.org

Hi, It looks like this now;

\<%form\_for :cats, :url =\> {:action =\> :save\_mt} do |form|%\> &nbsp;&nbsp;\<%@flokkar.each do |flokkur|%\> &nbsp;&nbsp;\<p\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= form.check\_box 'flokkur' %\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= flokkur %\> &nbsp;&nbsp;\</p\> &nbsp;&nbsp;\<%end%\> &nbsp;&nbsp;\<%= submit\_tag "Vista mælitæki", :class =\> 'submit'%\> \<%end%\>

I've tried different parameters for the check\_box but to no success...

BR, Sindri

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [June 10, 2010, 9:49am UTC](https://discuss.rubyonrails.org/t/check-box-tag-problems/52211/10 "2010-06-10T09:49:02Z")

</div>

Sindri Guðmundsson wrote:

> Marnen Laibow-Koser wrote:
> 
> > Sindri Guðmundsson wrote:
> > 
> > > If this helps these are the parameters passed when I use check\_box:
> > > 
> > > &nbsp;&nbsp;Parameters: {"commit"=\>"Vista mælitæki", "authenticity\_token"=\>"B2u5ULNr7IJ/ta0+hiAMBjmjEtTtc/yMAQQvSxFn2d0=", "cats"=\>{"flokkur"=\>[nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]}}
> > 
> > And what does that version of the view code look like?
> > 
> > Best, -- Marnen Laibow-Koser [http://www.marnen.org](http://www.marnen.org) marnen@marnen.org
> 
> Hi, It looks like this now;
> 
> \<%form\_for :cats, :url =\> {:action =\> :save\_mt} do |form|%\> &nbsp;&nbsp;\<%@flokkar.each do |flokkur|%\> &nbsp;&nbsp;\<p\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= form.check\_box 'flokkur' %\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= flokkur %\> &nbsp;&nbsp;\</p\> &nbsp;&nbsp;\<%end%\> &nbsp;&nbsp;\<%= submit\_tag "Vista mælitæki", :class =\> 'submit'%\> \<%end%\>
> 
> I've tried different parameters for the check\_box but to no success...
> 
> BR, Sindri

Finally got it, the empty hash did the trick 😉 Thanks though 🙂

\<%form\_for :cats, :url =\> {:action =\> :save\_mt} do |form|%\> &nbsp;&nbsp;\<%@flokkar.each do |flokkur|%\> &nbsp;&nbsp;\<p\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= form.check\_box flokkur, {}, "yes", "no" %\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= flokkur %\> &nbsp;&nbsp;\</p\> &nbsp;&nbsp;\<%end%\> &nbsp;&nbsp;\<%= submit\_tag "Vista mælitæki", :class =\> 'submit'%\> \<%end%\>
