Reputation: 6606
I'm trying to validate 2 fields which do no belong to the User
table
attr_accessor :terms_of_service, :privacy_policy
validates :terms_of_service, presence: true
validates :privacy_policy, presence: true
in the request sent from the client, the :terms_of_service
and :privacy_policy
arguments can either be absent, or a boolean value.
Rails needs to pass validation only if the value is true
(i.e send error only if parameter is absent or false)
however , for some reason - validation always fails regardless of the parameters being true or absent/false
I've even tried
validates_presence_of :terms_of_service
validates_presence_of :privacy_policy
and also
validates :terms_of_service, acceptance: true
validates :privacy_policy, acceptance: true
these are the params i'm sending to rails
{"username"=>"justin", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "terms_of_service"=>false, "privacy_policy"=>false, "confirm_success_url"=>"http://localhost:4000", "config_name"=>"default", "registration"=>{"username"=>"justin", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "terms_of_service"=>false, "privacy_policy"=>false, "confirm_success_url"=>"http://localhost:4000"}}
when i try to log the below using
puts "terms_of_service : #{:terms_of_service}"
i get the below output
terms_of_service : terms_of_service
Upvotes: 1
Views: 685
Reputation: 6606
The problem was that i did not properly sanitize my params
this is what i did in my applicationController.rb
before_action :configure_permitted_parameters, if: :devise_controller?
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:username,:terms_of_service,:privacy_policy])
end
Upvotes: 1
Reputation: 2927
You'll need to use a custom validator. presence: true
checks for the existence of the field so it would pass if its set to anything including false.
validate :privacy_policy_valid?
privacy
def privacy_policy?
errors.add(:privacy_policy "must be true") if privacy_policy
end
For your logging statement try
puts "terms_of_service : #{terms_of_service}"
Upvotes: 0