使用设计中的current_user选择用户名

时间:2017-11-29 21:20:17

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2

我读到可以在视图中调用帮助程序current_user。所以我有类似的东西

<h1> Customer id <%= User.select(:username).find(current_user).id %> </h1>

我在SQL中尝试做的相当于以下

  

SELECT username FROM users WHERE id = current_user

我不确定我做错了什么。我收到此错误消息:

  

未定义的局部变量或方法`current_user&#39;对于   ApplicationController中:类

1 个答案:

答案 0 :(得分:1)

由于您拥有来自current_user gem的Devise,因此您可以访问User实例对象,因此您可以直接执行以下操作:

<h1>Customer id <%= current_user.username %></h1>

如果您想显示id

<h1>Customer id <%= current_user.id %></h1>
相关问题