Rails & # 8594; Devise & # 8594; how to access current_user from module

I am currently using Rails 3.2.9 with development 3.0.4

I can access Devise's current_user in the controller without any problem.

However, when I try to access current_user in a module like below and get an error.

c_time = DateTime.current.in_time_zone(current_user.local_timezone)

      

NameError (undefined local variable or `current_user 'method for DateCalculator: Module):

It would be great if someone asks for advice on what is the best way to access current_user from a module.

Thank you very much in advance.

+3


source to share


3 answers


Try this instead of using the current_user object



UserSession.find.user

      

0


source


If your module does something specific for the current subscribed user, you can follow this path



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

      

0


source


There is a gem called sentient_user , this is a cheeky approach, but it does exactly what you need;), which should be able to access the currently logged in user, from the | Controller | Model, etc.

https://rubygems.org/gems/sentient_user

      

0


source







All Articles