2013-03-28 34 views
0

我想用當前用戶的employee_id更新表Tasks中的inspector_id。但是,該模型不會讓我訪問current_user。rails model before_update無法訪問current_user

這是我試圖在任務模型中使用的代碼。

before_update :update_inspector 

protected 
def update_inspector 
    self.inspector_id = current_user.employee 
end 

我得到:

NameError (undefined local variable or method `current_user' 

回答

1

通常CURRENT_USER在應用控制器中定義。這意味着該方法僅適用於控制器內部,不適用於模型。你所看到的錯誤與事態是一致的。如果你需要任務模型中的current_user,你必須在某個地方傳遞它。

0

您可以隨時訪問在模型內部構建current_user變量的會話信息。

相關問題