我正在尝试访问errors数组以在 View 中显示,但是我正在模型内的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之外(在模型本身中),错误可以正常工作。
最佳答案
设置值时,您必须在此处使用self.
accepts_nested_attributes_for :entries,
:reject_if => lambda {
"validation here"
self.errors[:base] = "You can't do that" #this line raises the above error
}
关于ruby-on-rails - 如何从模型的lambda中访问rails errors数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6390315/