2016-09-02 9 views
0

ApplicationControllerでは、devise docsによると、How To: Redirect to a specific page on successful sign in and sign outは、デバッグコンソールでさえもケーススイッチに到達できません。 'resource.class == User is is本当です。私が逃したRails処理のどの部分がわからないのですが、何かヒントをいただければ幸いです!Rails 4のdevise gemで、after_sign_in_path_forを無効にしても無効です

# ApplicationController.rb 
class ApplicationController < ActionController::Base 
    # Prevent CSRF attacks by raising an exception. 
    # For APIs, you may want to use :null_session instead. 
    protect_from_forgery with: :exception 


    protected 
    def after_sign_in_path_for(resource) 
    # check for the class of the object to determine what type it is 
    binding.pry 
    case resource.class 
    when User 
     puts "user redirect ==== " 
     return session.delete(:return_to) || current_user_path 
    else 
     puts "super call ....." 
     super 
    end 
    end 
end 

答えて

1

あなたはかなり近いです。 resource.class.nameを使用してリソースクラス名を取得するだけで、クラス名である'User'などの文字列と比較することができます。

def after_sign_in_path_for(resource) 
    # check for the class of the object to determine what type it is 
    binding.pry 
    case resource.class.name #=>this would return the class name i.e 'User' 
    when 'User' 
    puts "user redirect ==== " 
    return session.delete(:return_to) || current_user_path 
    else 
    puts "super call ....." 
    super 
    end 
end 
1

あなたはDevise::SessionsControllerから継承SessionsControllerを作成することで、回避策を行うことができます。このようなあなたのroutes.rb内部のそれに

class SessionsController < Devise::SessionsController 
    skip_before_filter :authenticate_user! 

    def create 
    user = User.find_for_database_authentication(email: params[:session][:email]) 

    if user && user.valid_password?(params[:session][:password]) 
     sign_in user 
     redirect_to session.delete(:return_to) || '/authorized' 
    else 
     redirect_to '/sign_in' 
    end 
    end 

    def destroy 
    sign_out :user 
    redirect_to '/signed_out' 
    end 
end 

ポイント:

devise_for :users, controllers: {sessions: 'sessions'} 
+0

ありがとうございました! ApplicationController#after_sign_in_path_forをオーバーライドするのではなく、SessionControllerのカスタマイズの背後にある考え方を説明することができれば、あなたの答えはより良いと思います。 – user746403

関連する問題