how to access user id value in another controller in ruby on rails [closed]
i want to access user login id (whenever he/she loggedin?) in some other controller i tried so many methods
Define a current_user
method in your ApplicationController
class ApplicationController < ActionController::Base
helper_method :current_user
def current_user
# ...
# return an instance of User class
# can be just:
# @current_user ||= User.find(session[:user_id])
# if you set session[:user_id] when the user logs in
end
end
This way you can access current_user.id
inside other controllers.
You need use session.
You put your user id in session after login
session[:user_id] = user.id
And in all of your controller you can fetch you userid from this session
User.find(session[:user_id])
精彩评论