如何让 "current_user"(编码为 `session[:user_id]`)显示用户电子邮件,而不是一些奇怪的代码?
How to have "current_user" (coded as `session[:user_id]`) show user email, instead of some weird code?
我遵循了有关 Rails 表单(登录、注销)的教程。一切顺利,除了这一点:用户登录后,页面底部应该显示 "You are logged in as",后跟电子邮件地址,例如 "You are logged in as Jane@aol.com"。代码行对此负责:
<p>You are logged in as: <%= @current_user %></p>
但是,我得到的是 You are logged in as: #<User:000s500r00k000h0>
(不是完全相同的副本)
我这辈子都搞不清楚那个号码是多少,为什么用户的电子邮件没有显示——也不知道从哪里开始进行故障排除:
current_user
定义在ApplicationController:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def authentication_required
if !logged_in?
redirect_to login_path
end
end
def logged_in?
!!current_user
end
def current_user
@current_user ||= begin
User.find(session[:user_id]) if session[:user_id]
end
end
helper_method :current_user
end
user_id
应该是用户的邮箱。我错过了什么?
只需将 <p>You are logged in as: <%= @current_user %></p>
更改为 <p>You are logged in as: <%= @current_user.user_id %></p>
。您当前看到的 #<User:000s500r00k000h0>
只是 User 对象本身的表示。
您应该从 @current_user
中指定您想要的内容:
<p>You are logged in as: <%= @current_user.email %></p>
如果您不指定,Rails 将直接抛出 @current_user
是什么。
首先你的一些代码似乎有误
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :authentication_required
def authentication_required
redirect_to login_path if !logged_in?
end
def logged_in?
current_user.present?
end
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user
end
您正在创建辅助方法,因此不需要调用 @current_user
,只需调用 current_user
此外,session[:user_id]
不是 email
。就是身份证您正在使用它来查找 User.find(session[:user_id])
的用户。这与 User.find(1)
相同。
#<User:000s500r00k000h0>
是对您分配给 @current_user
的用户对象的引用,数字是内存中存储它的 space。
您需要做的就是调用您希望为登录用户显示的方法。如果 email
那么 email
如果 username
那么 username
等等
我遵循了有关 Rails 表单(登录、注销)的教程。一切顺利,除了这一点:用户登录后,页面底部应该显示 "You are logged in as",后跟电子邮件地址,例如 "You are logged in as Jane@aol.com"。代码行对此负责:
<p>You are logged in as: <%= @current_user %></p>
但是,我得到的是 You are logged in as: #<User:000s500r00k000h0>
(不是完全相同的副本)
我这辈子都搞不清楚那个号码是多少,为什么用户的电子邮件没有显示——也不知道从哪里开始进行故障排除:
current_user
定义在ApplicationController:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def authentication_required
if !logged_in?
redirect_to login_path
end
end
def logged_in?
!!current_user
end
def current_user
@current_user ||= begin
User.find(session[:user_id]) if session[:user_id]
end
end
helper_method :current_user
end
user_id
应该是用户的邮箱。我错过了什么?
只需将 <p>You are logged in as: <%= @current_user %></p>
更改为 <p>You are logged in as: <%= @current_user.user_id %></p>
。您当前看到的 #<User:000s500r00k000h0>
只是 User 对象本身的表示。
您应该从 @current_user
中指定您想要的内容:
<p>You are logged in as: <%= @current_user.email %></p>
如果您不指定,Rails 将直接抛出 @current_user
是什么。
首先你的一些代码似乎有误
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :authentication_required
def authentication_required
redirect_to login_path if !logged_in?
end
def logged_in?
current_user.present?
end
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user
end
您正在创建辅助方法,因此不需要调用 @current_user
,只需调用 current_user
此外,session[:user_id]
不是 email
。就是身份证您正在使用它来查找 User.find(session[:user_id])
的用户。这与 User.find(1)
相同。
#<User:000s500r00k000h0>
是对您分配给 @current_user
的用户对象的引用,数字是内存中存储它的 space。
您需要做的就是调用您希望为登录用户显示的方法。如果 email
那么 email
如果 username
那么 username
等等