2011-06-17 107 views
1

我试图访问错误数组以显示在我的视图中,但我正在写入模型内的lambda内。我不断收到:如何从模型中的lambda中访问rails errors数组?

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 
    } 

拉姆达(模型本身)之外,误差正常工作。

回答

0

当你设置的值,你必须在这里使用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 
    } 
+0

其实我试过了,但它也不管用。我也试着直接在课堂上调用它。 – Jeff