Ruby on rails 无法从rails 5中的会话获取值
你好,在过去的几个小时里,我一直在努力解决这个问题,但我还是不知道哪里出了问题 我在Ruby on rails 无法从rails 5中的会话获取值,ruby-on-rails,ruby,session-cookies,ruby-on-rails-5,Ruby On Rails,Ruby,Session Cookies,Ruby On Rails 5,你好,在过去的几个小时里,我一直在努力解决这个问题,但我还是不知道哪里出了问题 我在sessioncontroller中使用user\u id设置会话,如下所示 class SessionsController < ApplicationController skip_before_action :authenticate, except: [:logout] def new @session = Session.new end def create @user_sessio
sessioncontroller
中使用user\u id
设置会话
,如下所示
class SessionsController < ApplicationController
skip_before_action :authenticate, except: [:logout]
def new
@session = Session.new
end
def create
@user_session = Session.new(session_params)
if @user_session.valid?
session[:user_id] = @user_session.user_id
redirect_to learners_url
else
redirect_to learner_new_registrations
end
end
def logout
session[:user_id] = nil
redirect_to root_url
end
private
def session_params
params.require(:session).permit(:email, :password)
end
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery
before_action :authenticate
def authenticate
return if signed_in?
redirect_to root_url
end
def current_user
@current_user ||= User.find(session[:user_id].to_s) #Problem is here. Here i am unable to access session[:user_id] which i was set in sessions controller. so it is redirecting to `root_url`
rescue Mongoid::Errors::DocumentNotFound => ex
end
def signed_in?
current_user.present?
end
helper_method :authenticate?
end
这意味着它正在重定向到LearnersController#Index
操作。在ApplicationController
中,我设置了一个回调
,如下所示
class SessionsController < ApplicationController
skip_before_action :authenticate, except: [:logout]
def new
@session = Session.new
end
def create
@user_session = Session.new(session_params)
if @user_session.valid?
session[:user_id] = @user_session.user_id
redirect_to learners_url
else
redirect_to learner_new_registrations
end
end
def logout
session[:user_id] = nil
redirect_to root_url
end
private
def session_params
params.require(:session).permit(:email, :password)
end
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery
before_action :authenticate
def authenticate
return if signed_in?
redirect_to root_url
end
def current_user
@current_user ||= User.find(session[:user_id].to_s) #Problem is here. Here i am unable to access session[:user_id] which i was set in sessions controller. so it is redirecting to `root_url`
rescue Mongoid::Errors::DocumentNotFound => ex
end
def signed_in?
current_user.present?
end
helper_method :authenticate?
end
class ApplicationControllerex
结束
def已登录?
当前用户是否存在?
结束
方法:验证?
结束
帮我解决它。为什么我无法在应用程序控制器中使用会话[:user\u id]
值?提前谢谢。
注意:我使用的是rails 5.0.0.beta3版本如果您从未创建过会话,请尝试以下操作:
def create
#@user_session = Session.create(session_params)
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
session[:user_id] = user.id
redirect_to learners_url
else
redirect_to learner_new_registrations
end
end
由于,您尚未对用户进行身份验证。因此,在应用程序控制器
中,有一个名为验证
的方法。它首先检查用户是否已登录。如果没有,则它会将您重定向到根url
def authenticate
return if signed_in?
redirect_to root_url
end
您从未创建会话,请尝试以下操作:
def create
#@user_session = Session.create(session_params)
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
session[:user_id] = user.id
redirect_to learners_url
else
redirect_to learner_new_registrations
end
end
由于,您尚未对用户进行身份验证。因此,在应用程序控制器
中,有一个名为验证
的方法。它首先检查用户是否已登录。如果没有,则它会将您重定向到根url
def authenticate
return if signed_in?
redirect_to root_url
end
这就是我在
sessioncontroller#Create
Action中所做的。区别是什么@dkpIndef current_user@current_user | |=user.find(session[:user_id]。to_s)#将session[:user_id]在我对用户进行身份验证后设置为零。rescue Mongoid::Errors::DocumentNotFound=>ex-end
会话[:user\u id]=@user\u session.user\u id
-在创建操作中有此语句。您应该像@user=user那样从数据库中获取用户。通过电子邮件(params[])查找:email
。然后您可以对用户进行身份验证,并将会话设置为session[:user_id]=@user.id
我已经通过在其中放入put语句进行了调试。我正在从数据库成功获取@user\u session.user\u id
值。甚至我也将该值设置为session。但它仍然不起作用@dkp这是我在sessioncontroller#Create
Action中所做的。区别是什么@dkpIndef current_user@current_user | |=user.find(session[:user_id]。to_s)#将session[:user_id]在我对用户进行身份验证后设置为零。rescue Mongoid::Errors::DocumentNotFound=>ex-end
会话[:user\u id]=@user\u session.user\u id
-在创建操作中有此语句。您应该像@user=user那样从数据库中获取用户。通过电子邮件(params[])查找:email
。然后您可以对用户进行身份验证,并将会话设置为session[:user_id]=@user.id
我已经通过在其中放入put语句进行了调试。我正在从数据库成功获取@user\u session.user\u id
值。甚至我也将该值设置为session。dkp仍然不起作用