How can you access the rails errors array from within a lambda in the model?
I'm trying to access the errors array to display in my view, but I'm writing to it inside a lambda within the model. I keep getting:
NameError Exception: undefined local variable or method `errors'
Here's my code for my model
accepts_nested_attributes_for :entries,
:reject_if => lambda {
"validation here"
errors[:base] = "You can't do开发者_C百科 that" #this line raises the above error
}
Outside of the lambda (in the model itself), the errors work correctly.
As you're setting the value, you'll have to use self.
here
accepts_nested_attributes_for :entries,
:reject_if => lambda {
"validation here"
self.errors[:base] = "You can't do that" #this line raises the above error
}
精彩评论