Jayaram
Jayaram

Reputation: 6606

Rails model validation - undefined method error

i'm trying to validate the presence on a parameter in rails. this is the code snippet for User.rb

  validates :terms_of_service, presence: true 
  validates :privacy_policy, presence: true

Ideally , i would like rails to throw an error if this parameter is not present

here is a sample parameter list sent by the client

Parameters: {"username"=>"f", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "confirm_success_url"=>"http://localhost:4000", "config_name"=>"default", "registration"=>{"username"=>"f", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "confirm_success_url"=>"http://localhost:4000"}}

However, rails throws the below error

NoMethodError (undefined method `terms_of_service' for #<User:0x007f16d402e788>):

why does it think that there should be a terms_of_service method when i'm actually testing a parameter?

Upvotes: 1

Views: 1218

Answers (1)

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

Add attr_accessor, it will set a getter and setter method and validation will work on it even if it is a non column attribute

attr_accessor :terms_of_service

Hope that helps!

Upvotes: 2

Related Questions