Question

I am experiencing an issue with validating our terms of service checkbox using devise and simple form.

User Model

validates_acceptance_of :terms, :allow_nil => false, :message => :terms_not_accepted, :on => :create

Registration View

= f.label :terms, "I agree to the #{link_to 'Terms of Service', "http://britevid.com/terms_of_service",:remote => true}.".html_safe, {class: "checkbox inline"}
= f.check_box :terms 
= f.button :submit, 'Sign up', :class => 'btn btn-success'

Application Controller

def update_sanitized_params
  devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:first_name, :last_name, :company, :phone_number, :website, :content_description, :address, :city, :state, :zip_code, :country, :paypal_email, :time_zone, :email, :current_password, :password, :password_confirmation, :terms)}
devise_parameter_sanitizer.for(:account_update) {|u| u.permit(:first_name, :last_name, :company, :phone_number, :website, :content_description, :address, :city, :state, :zip_code, :country, :paypal_email, :time_zone, :email, :current_password, :password, :password_confirmation)}
end

Started POST "/users" for 127.0.0.1 at 2014-03-21 07:19:24 -0500
Processing by RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "user"=>{"first_name"=>"jeff", "last_name"=>"smith", "email"=>"jeffsmith@lol.com", "company"=>"test", "phone_number"=>"453234562", "website"=>"", "content_description"=>"test", "address"=>"1st ave", "city"=>"big city", "state"=>"California", "country"=>"United States", "zip_code"=>"095610", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "terms"=>"0"}, "commit"=>"Sign up"}
   (0.2ms)  BEGIN
  User Exists (0.1ms)  SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'jeffsmith@lol.com' LIMIT 1
   (0.1ms)  ROLLBACK
  Rendered devise/registrations/new.html.haml within layouts/pages (46.2ms)
  Rendered layouts/shared/_external_header.html.haml (0.4ms)
Completed 200 OK in 157ms (Views: 67.7ms | ActiveRecord: 0.4ms)
Was it helpful?

Solution

You have setup a validation on terms

validates_acceptance_of :terms, :allow_nil => false, :message => :terms_not_accepted, :on => :create

So, its mandatory to check the terms checkbox while creating a new User. If you don't select the checkbox then your validation fails and you get the error. And ofcourse user record doesn't get saved.

As per the server log, while submitting the form you didn't select the terms checkbox as "terms"=>"0"(in params hash).

Make sure that you select the terms before submitting the form.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top