简体   繁体   中英

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"]

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.

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.

我不得不使用这种格式:

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

I have tried this from Angular JS and Rails 4. Angular send parameter with true value but rails did not recognize true .

It can receive an :accept option, which determines the value that will be considered acceptance. It defaults to "1" and can be easily changed.

So, I change into this:

class Person < ActiveRecord::Base
  validates :terms_of_service, acceptance: { accept: true }
end

If you have default parameter is 1 or 0 . Try to do this:

class Person < ActiveRecord::Base
  validates :terms_of_service, acceptance: true
end

This is for more documentation. acceptance validation . I hope this help you.

I found Candland's answer above for validates acceptance to be correct in Rails 3.1. This is how I set up my Rails 3.1.3 app to record the acceptance to the database.

In the migration,

class AddTermsToAccount < ActiveRecord::Migration
  def change
    add_column :accounts, :terms_of_service, :boolean, :default => false
  end
end

In the model,

attr_accessible :terms_of_service
validates :terms_of_service, :acceptance => {:accept => true}

In the form,

<%= f.check_box :terms_of_service %>
<%= f.label :terms_of_service %>

if you have a basic checkbox in your view, such as

<%= builder.check_box :agreement %>

just put this line in your model

validates :agreement, :acceptance => true

which uses the default "1" generated by the check_box view helper

A call to a factory creates the record, so your subsequent calls to save are in fact updates, so your validation fails as intended. Try it without the factory and it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM