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

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?

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

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.

could you use 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. [email protected]