在设计中获取当前用户ID

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

在我的控制器中我有这样的事情:

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

我收到一条错误,说“无法找到没有ID的用户”。

如果我在User.find()中输入一个静态数字它可以工作,但当然这不是我想要的。 任何帮助将不胜感激。

谢谢!

替换当前索引操作如下

 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,则用户将被重定向到带有flash消息的登录页面。