我正在尝试验证rails中参数的存在。这是free(ptr)
User.rb
理想情况下,如果此参数不存在,我希望rails抛出错误
这是客户端发送的示例参数列表
validates :terms_of_service, presence: true
validates :privacy_policy, presence: true
但是,rails会抛出以下错误
Parameters: {"username"=>"f", "email"=>"f@f.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "confirm_success_url"=>"http://localhost:4000", "config_name"=>"default", "registration"=>{"username"=>"f", "email"=>"f@f.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "confirm_success_url"=>"http://localhost:4000"}}
为什么我认为在实际测试参数时应该有NoMethodError (undefined method `terms_of_service' for #<User:0x007f16d402e788>):
方法?
答案 0 :(得分:1)
添加attr_accessor
,它会设置一个getter和setter方法,即使它是非列属性,验证也会对它起作用
attr_accessor :terms_of_service
希望有所帮助!