views:

190

answers:

4

I need a "I accept terms of service" checkbox on a page, it has to be checked in order for the order to proceed. It seems hence illogical to have a column in the database to match this (whether user has accepted or declined terms).

I am using the form helper like this in my view:

<%= check_box("client", "terms") %>

And in my model:

validates_acceptance_of :terms

At the moment it is not working at all.

This seems like a really common piece of code, yet I can't find it used anywhere without having the terms in the model. Else I could use javascript to validate it, but would prefer to keep it all the in model.

+3  A: 

What about having an attr_accessor :terms in your Client model?

neutrino
+1  A: 

This should work fine, without a database column or attr_accessor: http://guides.rubyonrails.org/activerecord_validations_callbacks.html#validates-acceptance-of

I would be inclined to check your params hash is as it should be i.e. that the 'terms' attribute is being passed within the 'client' hash, perhaps try a adding raise params.inspect on your controller create action to help you debug?

Paul Groves
Cheers, this worked great - and I learnt a bit about degbugging!
vectran
A: 

Either go with @neutrino's solution, or to reset :terms to "not accepted" if you need to redisplay the form (because validation may fail), use this:

def terms
  nil
end

def terms=(val)
  # do nothing
end
hurikhan77
A: 

attr_accessor :terms will do the trick nicely.

BandsOnABudget