2012-04-10 13 views
0

私はDeviseを使用しています。私はログアウトアクションを行いたいと思います。Rails Devise 2.0独自のログアウトアクション

私がやりたいことは、ログアウトするときに、自分自身のJSONオブジェクトを作成して戻すことです。この時点で、私がログアウトした後、私はすべてのルート記事を取得します。

作成アクションを見つけたように、自分のdestoryアクションを作成するにはどうすればよいですか? routes.rbを

devise_for :users, :controllers => {:session => "sessions"} do 
    get "https://stackoverflow.com/users/sing_out" => "devise/sessions#destroy" 
    end 

答えて

0

class SessionsController < Devise::SessionsController 

    def create 
    resource = warden.authenticate!(:scope => resource_name, :recall => :failure) 
    return sign_in_and_redirect(resource_name, resource) 
    end 

    def sign_in_and_redirect(resource_or_scope, resource=nil) 
    scope = Devise::Mapping.find_scope!(resource_or_scope) 
    resource ||= resource_or_scope 
    sign_in(scope, resource) unless warden.user(scope) == resource 
    return render :json => {:success => true, :redirect => stored_location_for(scope) || after_sign_in_path_for(resource)} 
    end 

    def failure 
    return render:json => {:success => false, :errors => ["Login failed."]} 
    end 

end 

そして、私のルートこれは、セッション・コントローラのdestroyメソッドです。

必要に応じてカスタマイズする必要があります。私は、別のアクションを追加してカスタムビヘイビアを実装する方が賢明だと思っています。これは、将来deviseをアップグレードする際に予期しないエラーが発生する可能性は低いからです。

# DELETE /resource/sign_out 
    def destroy 
    redirect_path = after_sign_out_path_for(resource_name) 
    signed_out = (Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)) 
    set_flash_message :notice, :signed_out if signed_out 

    # We actually need to hardcode this as Rails default responder doesn't 
    # support returning empty response on GET request 
    respond_to do |format| 
     format.any(*navigational_formats) { redirect_to redirect_path } 
     format.all do 
     method = "to_#{request_format}" 
     text = {}.respond_to?(method) ? {}.send(method) : "" 
     render :text => text, :status => :ok 
     end 
    end 
    end 
+0

これは私のために動作しませんいいえ、私の問題は、私は今、自分のクラスSessionsControllerを持っていること、である<考案:: SessionsControllerが、どのように私は私のsessioncontrollerへのログインとログアウトをリダイレクトすることができますか? – Lailo

+0

アプリケーションにセッションコントローラがある場合は、すべての要求に応答する必要があります。リダイレクトする必要はありません。 – phoet

+0

Okai、セッションコントローラへのルーティングを削除しましたが、それでも動作しません。私のsessions_controller.rbは私が投稿したクラスのようです。何が間違っていますか? – Lailo

関連する問題