Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validates acceptance always failing

I can't see what I'm missing, but something is obviously not right.

In model:

validates :terms, :acceptance => true, :on => :update 

Trying a few options:

>> a = Factory(:blog_agreement) => #<BlogAgreement id: 54, terms: false, created_at: "2011-01-20 11:33:03", updated_at: "2011-01-20 11:33:03", accept_code: "fa27698206bb15a6fba41857f12841c363c0e291", user_id: 874>  >> a.terms => false  >> a.terms = true => true >> a.save => false  >> a.terms = "1" => "1" >> a.save => false  >> a.terms = 1 => 1 >> a.save => false >> a.errors.full_messages => ["Terms must be accepted"] 
like image 432
user582874 Avatar asked Jan 20 '11 11:01

user582874


People also ask

What is the difference between validate and validates in rails?

validates is used for normal validations presence , length , and the like. validate is used for custom validation methods validate_name_starts_with_a , or whatever crazy method you come up with. These methods are clearly useful and help keep data clean.

What is validate in Ruby on Rails?

Validations are used to ensure that only valid data is saved into your database. For example, it may be important to your application to ensure that every user provides a valid email address and mailing address. Model-level validations are the best way to ensure that only valid data is saved into your database.


2 Answers

Updated answer..

So it turns out that the problem was having terms as an actual column in the table. In general validates_acceptance_of is used without such a column, in which case it defines an attribute accessor and uses that for its validation.

In order for validates_acceptance_of to work when it maps to a real table column it is necessary to pass the :accept option, like:

validates :terms, :acceptance => {:accept => true} 

The reason for this has to do with typecasting in Active Record. When the named attribute actually exists, AR performs typecasting based on the database column type. In most cases the acceptance column will be defined as a boolean and so model_object.terms will return true or false.

When there's no such column attr_accessor :terms simply returns the value passed in to the model object from the params hash which will normally be "1" from a checkbox field.

like image 157
noodl Avatar answered Oct 06 '22 00:10

noodl


In the case of someone has the same problem like me with devise, i add this answer:

i added to the devise's registration form:

sign_up.html.erb

<%= f.check_box :terms_of_service %> 

user.rb

validates, :terms_of_service, acceptance: true 

i forgot to add :terms_of_service inside my configured_permitted_parameters and devise ignored the checkbox state.

application_controller.rb

before_filter :configure_permitted_parameters, if: :devise_controller?  def configure_permitted_parameters   devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :terms_of_service)} end 

The configure_permitted_parameters method is used by devise for know what params he should be save in addition of email and password.

like image 28
Ayoros Avatar answered Oct 06 '22 02:10

Ayoros