2012-05-03 9 views

答えて

1

、あなたはafter_sign_in_path私はそうすることを発見した最も簡単なメソッドをオーバーライドする必要があります、次の通りである:あなたがする必要があります

アプリケーションコントローラの内部二つの方法、

include SessionsHelper 
    def after_sign_in_path_for(resource_or_scope) 
    case resource_or_scope 
    when :user, User 
     store_location = session[:return_to] 
     clear_stored_location 
     (store_location.nil?) ? requests_path : store_location.to_s 
    else 
     super 
    end 
    end 

    def check_login 
    if !anyone_signed_in? 
     deny_access 
    end 
    end 

ファーストを追加当社はに定義されます私たちはstore_locationにRailsのセッションから引っ張って私たちの新しい保存場所を保存しafter_sign_in_pathを上書き。次に、これを使用したいコントローラーでbefore_filterとして使用できるメソッドを作成します。

次に、私達はちょうど私たちはすべてのかなり自明である必要があり、当社のアプリケーションコントローラの内部で使用されるメソッドを定義している。ここ

module SessionsHelper 

    def deny_access 
    store_location 
    redirect_to new_user_session_path 
    end 

    def anyone_signed_in? 
    !current_user.nil? 
    end 

    private 

    def store_location 
     session[:return_to] = request.fullpath 
    end 

    def clear_stored_location 
     session[:return_to] = nil 
    end 

end 

sessions_helper.rbを設定します。前のパスを覚えておきたいコントローラのフィルタの前に、before_filter :check_loginを使用してください。

関連する問題