# "If the checkbox is checked, then do......."

**URL:** <https://www.ruby-forum.com/t/if-the-checkbox-is-checked-then-do/121367>\
**Category:** Rails\
**Created:** [November 27, 2007, 10:27am UTC](https://www.ruby-forum.com/t/if-the-checkbox-is-checked-then-do/121367 "2007-11-27T10:27:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [November 27, 2007, 10:27am UTC](https://www.ruby-forum.com/t/if-the-checkbox-is-checked-then-do/121367/1 "2007-11-27T10:27:43Z")

</div>

Hello,

I’m incorporating a small form that works like a “terms of service  
agreement”, and want to do XYZ if the checkbox is checked.

I have this:

\<%= check\_box\_tag “user\_agreement[]” %\>

Now, is this the correct way to see if the checkbox is indeed checked:

if params[:user\_agreement] == true  
…  
end

Am I on the right path? Or, is there a better way to do it?

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [November 27, 2007, 10:41am UTC](https://www.ruby-forum.com/t/if-the-checkbox-is-checked-then-do/121367/2 "2007-11-27T10:41:27Z")

</div>

the params that’s handed back is a string, containing ‘0’ or ‘1’ (you  
could modify these values, if necessary)  
so

if params[:user\_agreement] == true  
will fail

if params[:user\_agreement]  
will fail the other way around (always true)

there are two ways, the simple:

if params[:user\_agreement] == ‘1’  
…  
end

should work.  
if you store the result in your db, you can use  
model.user\_agreement  
after saving it or using update\_attributes  
then you would have a ‘real’ bool, which would work es expected

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [November 27, 2007, 10:57am UTC](https://www.ruby-forum.com/t/if-the-checkbox-is-checked-then-do/121367/3 "2007-11-27T10:57:44Z")

</div>

Thanks for the great insight, Thorsten!

It looks like storing the result in the db is the best way to do it, and  
much more efficient than my lousy approach. Thanks a bunch again for the  
help.

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [November 27, 2007, 3:48pm UTC](https://www.ruby-forum.com/t/if-the-checkbox-is-checked-then-do/121367/4 "2007-11-27T15:48:19Z")

</div>

could you use validates\_acceptance\_of

[http://railsmanual.com/module/ActiveRecord::Validations::ClassMethods/validates\_acceptance\_of](http://railsmanual.com/module/ActiveRecord::Validations::ClassMethods/validates_acceptance_of)  
or page 366 in Agile Web D. with Rails 2ed.

John.

On Nov 27, 3:57 am, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
