こんにちは私は過去数時間からこれに苦労していますが、間違ったところが分からない。セッションの値をレールで取得できません5
私はそれがlearners_url
にリダイレクトされるセッション値を設定した後create
この動作
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
エンド
ようSessionsController
にuser_id
とsession
を設定していますが、やはりURLをrootにリダイレクトされます。次server log
Started POST "/sessions" for 127.0.0.1 at 2016-04-13 12:00:01 +0530
Processing by SessionsController#create as HTML
Parameters: {"session"=>{"email"=>"[email protected]", "password"=>"[FILTERED]"}, "login-submit"=>"Submit"}
Can't verify CSRF token authenticity
MONGODB | localhost:27017 | project_practice_development.find | STARTED | {"find"=>"users", "filter"=>{"email"=>"[email protected]"}, "limit"=>-1}
MONGODB | localhost:27017 | project_practice_development.find | SUCCEEDED | 0.000360343s
Redirected to http://localhost:3000/learners
Completed 302 Found in 67ms
Started GET "/learners" for 127.0.0.1 at 2016-04-13 12:00:01 +0530
Processing by LearnersController#index as HTML
MONGODB | localhost:27017 | project_practice_development.find | STARTED | {"find"=>"users", "filter"=>{"_id"=>nil}}
MONGODB | localhost:27017 | project_practice_development.find | SUCCEEDED | 0.000387588s
Redirected to http://localhost:3000/
Filter chain halted as :authenticate rendered or redirected
Completed 302 Found in 2ms
Started GET "/" for 127.0.0.1 at 2016-04-13 12:00:01 +0530
Processing by WelcomesController#about as HTML
Rendered welcomes/about.html.erb within layouts/welcomes (0.2ms)
Completed 200 OK in 16ms (Views: 15.4ms)
守っそれがLearnersController#Index
アクションにリダイレクトされることを意味します。 ApplicationController
では私はそれを解決するために私を助けてcall back
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
を設定しています。なぜアプリケーションコントローラでsession[:user_id]
の値を使用できなかったのですか?前もって感謝します。 注:ユーザーを認証していない、ので
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
:私はこれを試してみてください、あなたがセッションを作成したことはありません5.0.0.beta3バージョン
これは、私が 'SessionsController#Create'アクションで行ったことです。 diffrenceは何ですか? @dkp –
の 'def current_user @current_user || = User.find(セッション[:user_id] .to_s)#puts session [:user_id]私は認証されたユーザーの後に私にnilを与えます。 レスキューMongoid :: Errors :: DocumentNotFound => ex end' –
'session [:user_id] = @ user_session.user_id' - あなたの作成アクションにこのステートメントがあります。 '@user = User.find_by_email(params []):email'のようにDBからユーザを取得する必要があります。次に、ユーザを認証し、 'session [:user_id] = @ user.id'のようなセッションを設定することができます。 – dp7