如何从模型访问devise current_user

Ton*_*shi 5 authentication devise

刚开始使用设计开发我的应用程序身份验证,但是从模型访问current_user时遇到了麻烦.我从控制器访问它没有问题.

对于我的场景,我需要获取当前用户ID以跟踪谁编辑了博客文章,我使用before_save过滤器保存了用户ID.

感谢您的帮助!

谢谢

m4r*_*isU 3

您只能将其传递到参数中。

class SomeModel 
  def my_nifty_method(user)
  end
end
Run Code Online (Sandbox Code Playgroud)

在控制器中:

class SomeModelsController < ApplicationController
  def some_method
    sm = SomeModel.new
    sm.my_nifty_method(current_user)
  end
end
Run Code Online (Sandbox Code Playgroud)