Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding TOS agreement checkbox with Devise

We're using devise for our members. We've added a tos_agreement field (boolean) to our member schema, and we added it to views/devise/registrations/new.html.haml.

In the Member model, we have a validation rule as follows:

validates_acceptance_of :tos_agreement, :allow_nil => false,
  :accept => true

It works fine - if you don't accept the TOS, you can't register.

However, the problem comes with editing your settings. If you go to /members/edit you get the form where you can change your email address or password. There's no field for the TOS agreement since this shouldn't be changeable at this point. However, when you make a change (eg. change your password) and submit the form, it returns an error message that the TOS agreement can't be false.

How can we tell it never to attempt to modify the TOS agreement after the first registration?

Edit: so I think the fundamental problem is that we had :tos_agreement in our attr_accessible, which was a very bad idea now I think of it. But if we remove it, how do we modify Devise to accept the parameter and do something with it even though it's not mass-assignable?

like image 214
Skud Avatar asked Feb 06 '13 02:02

Skud


1 Answers

You can pass an :on => :create option to the validator so that it's only checked on signup:

validates_acceptance_of :tos_agreement, :allow_nil => false, :accept => true, :on => :create
like image 151
Nancy Avatar answered Oct 16 '22 22:10

Nancy