开发者

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
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜