Code Tree
Code Tree

Reputation: 2209

validates_acceptance_of not working for a checkbox

I am trying various options for the past three hours to make a "terms and conditions" checkbox working

Here is my code /models/user.rb

class User < ActiveRecord::Base
  #validates :terms, acceptance: { accept: true } , :on => :create
  validates_acceptance_of :terms, :allow_nil => false,  :accept => true, :on => :create
end

this is what I am always getting

html looks like this

I added this to allow params

class ApplicationController < ActionController::Base

  before_action :configure_permitted_parameters, if: :devise_controller?
  # for allowing terms in signup 
  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:terms])
  end

end

'rails', '4.2.8'

Upvotes: 0

Views: 204

Answers (1)

alex
alex

Reputation: 3742

You're passing 1 as a checkbox value. It's a default for the validator. So all you need is to get rid of :accept => true option. The true one is used only in case of validating boolean database field. As I understand you don't have such a column in users table and only want to referer to the HTML checkbox.

Upvotes: 1

Related Questions