Ruby on rails 更新前的rails模型可以';无法访问当前用户
我想用当前用户的员工id更新表Tasks中的inspector_id。但是,该模型不允许我访问当前用户 这是我试图在任务模型中使用的代码Ruby on rails 更新前的rails模型可以';无法访问当前用户,ruby-on-rails,Ruby On Rails,我想用当前用户的员工id更新表Tasks中的inspector_id。但是,该模型不允许我访问当前用户 这是我试图在任务模型中使用的代码 before_update :update_inspector protected def update_inspector self.inspector_id = current_user.employee end 我得到: NameError (undefined local variable or method `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'
通常,当前用户在应用程序控制器中定义。这意味着该方法仅在控制器内部可用,而不在模型中可用。你看到的错误与这种情况是一致的。如果您需要任务模型中的当前用户,则必须将其传入某个位置。您始终可以访问在模型中生成当前用户变量的会话信息