开发者

how to access user id value in another controller in ruby on rails [closed]

开发者_JAVA百科 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

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])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜