Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 更新前的rails模型可以';无法访问当前用户_Ruby On Rails - Fatal编程技术网

Ruby on rails 更新前的rails模型可以';无法访问当前用户

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'

我想用当前用户的员工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' 

通常,当前用户在应用程序控制器中定义。这意味着该方法仅在控制器内部可用,而不在模型中可用。你看到的错误与这种情况是一致的。如果您需要任务模型中的当前用户,则必须将其传入某个位置。

您始终可以访问在模型中生成当前用户变量的会话信息