I'm currently using Rails 3.2.9 with devise 3.0.4
I can access Devise current_user in Controller without any problem.
However, when I'm trying to access current_user in Module as below and receive error.
c_time = DateTime.current.in_time_zone(current_user.local_timezone)
NameError (undefined local variable or method `current_user' for DateCalculator:Module):
It would be great if someone please advise what's the best way to access current_user from Module.
Many Thanks in advance.
试试这个而不是使用current_user对象
UserSession.find.user
If your module does something specific to current signed in user, your can follow this way
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
There is a Gem called sentient_user it is a cheeky approach but it does exactly what you need ;) which is to have access to current logged in user, from view | Controller | Model etc
https://rubygems.org/gems/sentient_user
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.