Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current user id in devise

How do I Get the current user id in my controller using devise?

In my controller I have something like this:

def index

me = current_user
c = User.find(me)
@sheets = c.time_sheets
end

I am getting an error say that "Couldn't find User without an ID".

If If I put in a static number in the User.find() it works but of course that is not what I want. Any help would be greatly appreciated.

thanks!

like image 223
mild0d Avatar asked Feb 28 '14 22:02

mild0d


1 Answers

Replace current index action as below

def index    
   if current_user
     @sheets = current_user.time_sheets
   else
     redirect_to new_user_session_path, notice: 'You are not logged in.'
   end
end

If user is not logged in, i.e., current_user=nil then user would be redirected to login page with a flash message.

like image 193
Kirti Thorat Avatar answered Nov 03 '22 16:11

Kirti Thorat