获取设计中的当前用户ID [英] Get current user id in devise

查看:196
本文介绍了获取设计中的当前用户ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用devise获取控制器中的当前用户ID?

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

我收到一个错误,表示无法找到没有ID的用户 。

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

如果我在User.find()中放入一个静态号码,那么它的工作原理当然不是我想要的。任何帮助将不胜感激。

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.

谢谢!

推荐答案

当前索引操作如下

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

如果用户未登录,即current_user = nil,则用户将被重定向到具有闪烁消息的登录页面。

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

这篇关于获取设计中的当前用户ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆