Set current_user in routes.rb

Is there a way to access the current user in routes.rb? I would like to do the following:

match /profile => redirect("/profiles/{current_user.name}") 

env['warden'] does not seem to be configured, so I cannot access warden.user.name .

+7
source share
3 answers

To deploy to user1136228 the answer:

 get '/profile', to: redirect { |params, request| me = request.env["warden"].user(:user) me ? ('/profiles/' + me.name) : '/' } 
+4
source

Not sure if this is possible. However, you can use the controller:

 def profile if signed_in? redirect_to user_profile_path(current_user) else redirect_to root_url end end 
+7
source

These are two possibilities. If you use the program, you can use the warden as follows:

 current_user = request.env["warden"].user(:user) 

or without overseer

 User.find_by_id(request.session[:user_id]) 

if you save your user id in the session: user_id

+2
source

All Articles