2011-06-17 33 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