1

I want to do something like this:

validates_presence_of :name, :message => custom_message

def custom_message
  "Custom Message"
end

But when I try it I get "undefined local variable or method for custom method"

Whats up with that?

Cameron
  • 4,181
  • 9
  • 36
  • 40

4 Answers4

3

Think I figured it out. If you use a symbol instead of the method directly.

e.g :message => :custom_message

Cameron
  • 4,181
  • 9
  • 36
  • 40
1

I got it working like this:

def self.custom_message
  "Custom Message"
end

validates_presence_of :name, message: custom_message
Derek Hill
  • 5,965
  • 5
  • 55
  • 74
0

try this ,, if im not wrong

before_save :custom_message def custom_message self.custom_message = "Custom Message" if published == true end end

Ahmad Ramdani
  • 208
  • 4
  • 14
  • hmmm not sure why I would want to do that... just wanna know why the validates method doesnt't see the local method – Cameron Aug 01 '09 at 14:29
0

Have you tried putting the definition of custom_message before the validates_presence_of line? validates_presence_of is a class method, and when it is evaluated Ruby has not yet seen the definition below it.