我在 Rails 应用程序的 lib 目录中创建了一个类。该类需要访问 Devise 的 current_user。
我还应该指出,这个 lib 类经常从模型代码中访问,因此不能将 current_user 传递给它。
我怎样才能做到这一点?
彼得。
我在 Rails 应用程序的 lib 目录中创建了一个类。该类需要访问 Devise 的 current_user。
我还应该指出,这个 lib 类经常从模型代码中访问,因此不能将 current_user 传递给它。
我怎样才能做到这一点?
彼得。
Where are you using your library? Are you calling it from any controller method? If so, you should pass the current user as a parameter to your library class constructor, and work with it there. current_user is a controller helper. It expects a valid authenticated session and therefore only makes sense when used from there and in the app flow. Using current_user for any background/script task would not make sense, anyway.