2016-04-13 20 views
0

こんにちは私は過去数時間からこれに苦労していますが、間違ったところが分からない。セッションの値をレールで取得できません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 

エンド

ようSessionsControlleruser_idsessionを設定していますが、やはり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バージョン

答えて

0

レールを使用しています。したがって、ApplicationControllerにはauthenticateという名前のメソッドがあります。最初にユーザーがsigned_in?かどうかをチェックします。そうでない場合は、root_urlにリダイレクトされます。

def authenticate 
    return if signed_in? 
    redirect_to root_url 
end 
+0

これは、私が 'SessionsController#Create'アクションで行ったことです。 diffrenceは何ですか? @dkp –

+0

の 'def current_user @current_user || = User.find(セッション[:user_id] .to_s)#puts session [:user_id]私は認証されたユーザーの後に私にnilを与えます。 レスキューMongoid :: Errors :: DocumentNotFound => ex end' –

+0

'session [:user_id] = @ user_session.user_id' - あなたの作成アクションにこのステートメントがあります。 '@user = User.find_by_email(params []):email'のようにDBからユーザを取得する必要があります。次に、ユーザを認証し、 'session [:user_id] = @ user.id'のようなセッションを設定することができます。 – dp7

関連する問題