我想在我的RoR应用程序中打印当前用户的电子邮件。为此,我也使用了下面的代码:如何在Devise中定义current_user?
User.current_user
,并打印下一个错误:undefined method
CURRENT_USER”的#`
,但是当我用刚刚current_user
它不打印任何东西。我在Google和Stack中搜索过,试图使用它们的答案,但没有任何结果。
如何获取用户电子邮件?
我想在我的RoR应用程序中打印当前用户的电子邮件。为此,我也使用了下面的代码:如何在Devise中定义current_user?
User.current_user
,并打印下一个错误:undefined method
CURRENT_USER”的#`
,但是当我用刚刚current_user
它不打印任何东西。我在Google和Stack中搜索过,试图使用它们的答案,但没有任何结果。
如何获取用户电子邮件?
在控制器中,仅current_user将返回当前登录的用户。因此,current_user.email将返回signed_in用户的电子邮件。对于未登录的用户,current_user将返回nil。 要在控制器中打印当前用户的电子邮件,
class TestController < ApplicationController
def example
p current_user.try(:email) # try is used because it will return nil if the user is signed in. otherwise, it will raise an error undefined method 'user' for nil class
end
end
试试这个。
User.current_user.email