[英]Rails -> Devise -> how to access current_user from Module
我目前正在使用Rails 3.2.9和devise 3.0.4
我可以在Controller中访问Devise current_user,没有任何问题。
但是,当我尝试访问Module中的current_user时,如下所示并收到错误。
c_time = DateTime.current.in_time_zone(current_user.local_timezone)
NameError(未定义的局部变量或DateCalculator:Module的方法`current_user'):
如果有人请告知从Module访问current_user的最佳方法,那将是很棒的。
提前谢谢了。
试试这个而不是使用current_user对象
UserSession.find.user
如果您的模块执行特定于当前登录用户的某些操作,您可以按照这种方式操作
config/initializers/user_specific_time.rb
module Devise
module Controllers
module Helpers
def user_specific_time
c_time = DateTime.current.in_time_zone(current_user.local_timezone)
end
end
end
end
有一个名为sentient_user的Gem它是一种厚颜无耻的方法,但它完全符合您的需要;)可以访问当前登录的用户, 控制器| 模型等
https://rubygems.org/gems/sentient_user
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.