gpt4 book ai didi

ruby-on-rails - 如何从模型的lambda中访问rails errors数组?

转载 作者:行者123 更新时间:2023-12-03 08:01:56 26 4
gpt4 key购买 nike

我正在尝试访问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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com