如何从模型中的 lambda 内部访问 Rails 错误数组?
我正在尝试访问错误数组以显示在我的视图中,但我正在模型内的 lambda 内写入它。我不断收到:
NameError Exception: undefined local variable or method `errors'
这是我的模型代码
accepts_nested_attributes_for :entries,
:reject_if => lambda {
"validation here"
errors[:base] = "You can't do that" #this line raises the above error
}
在 lambda 之外(在模型本身中),错误正常工作。
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 that" #this line raises the above error
}
Outside of the lambda (in the model itself), the errors work correctly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您设置该值时,您必须在此处使用
self.
As you're setting the value, you'll have to use
self.
here