I am trying to apply a special validator to my issue.rb model:
class Issue < ActiveRecord::Base attr_accessible :description, :no_followers, :title validates_presence_of :title validates_uniqueness_of :title, message: "Title should be unique!" validates_length_of :description, minimum: 10, maximum: 50 validates_numericality_of :no_followers, allow_blank: true validates_with YesNoValidator end
A validator is a file located in an application / validator and containing the following:
class YesNoValidator < ActiveModel::Validator def validate record if record.title.include? "yes" && record.description.include? "No" record.errors[:title] << "Title has the word yes and description has the word no" end end end
I also tried putting it in the lib folder, but this also gives this error:
Routing Error uninitialized constant Issue::YesNoValidator
Accidentally F5'ing sometimes I get this error:
NoMethodError in IssuesController
So it looks like the class file is not loaded, so I tried to add both lib and the app / validators folder to autoload_paths in application.rb. But that doesn't work either.
Has anyone experienced this before?
source share