views:

32

answers:

2

i want to access user login id (whenever he/she loggedin?) in some other controller i tried so many methods

+1  A: 

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.

j.
You don't answer the question because it's assume he use some authentication plugin
shingara
I didn't assume he's using any plugins. If he has a login action, he's keeping the `user_id` somewhere. This is everything I've assumed.
j.
Yes, regardless of whether he's using an authentication plugin, it's still perfectly valid to define `current_user`, and more than likely he is using a plugin anyway.
Karl
+1  A: 

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])
shingara
Please see my comment in my answer to reconsider your down vote. tks.
j.