where to put ActiveModel::Validator?
Asked Answered
T

3

22

I try to follow http://api.rubyonrails.org/classes/ActiveModel/Validator.html , but where should I put the

 class MyValidator < ActiveModel::Validator
    def validate(record)
      if some_complex_logic
        record.errors[:base] = "This record is invalid"
      end
    end

    private
      def some_complex_logic
        # ...
      end
  end
Theodoretheodoric answered 17/12, 2010 at 7:53 Comment(0)
A
25

This guy puts them under app/validators/, which I've done as well, ever since I saw that blog post.

Alica answered 16/5, 2011 at 16:43 Comment(1)
Any chance anyone knows how to display this folder in "Project" structure in Netbeans? I'm currently only able to access this folder through "Files", but i hate the tree hierarchy.Erkan
A
2

Add this class in your lib directory and require it in your model and include it inside.

Anamorphism answered 17/12, 2010 at 8:8 Comment(0)
F
1

Alternatively, you can also add it to the models directory of your app. Also, as mentioned by shingara, you need to add,

include ActiveModel::Validations
validates_with MyValidator 

to the model file of the record class.

Fretwell answered 17/4, 2011 at 3:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.