Get current user id in devise

前端 未结 1 1953
一向
一向 2021-02-19 20:04

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(m         


        
1条回答
  •  爱一瞬间的悲伤
    2021-02-19 20:37

    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.

    0 讨论(0)
提交回复
热议问题