After a successful DEVISE login, how to redirect user back to previous action that required login?

后端 未结 3 898
旧时难觅i
旧时难觅i 2021-02-05 08:21

I have an ajax voting button: If a user clicks on a \"thumbs up\" image, but is not logged in, then they should see a dialog box that asks them to login first.

To do thi

3条回答
  •  春和景丽
    2021-02-05 08:44

    I do the following with Authlogic on Rails 3, perhaps you could use it or do something similar:

    class ApplicationController < ActionController::Base
      after_filter :store_location
    private
      def store_location
        session[:return_to] = request.fullpath
      end
    
      def clear_stored_location
        session[:return_to] = nil
      end
    
      def redirect_back_or_to(alternate)
        redirect_to(session[:return_to] || alternate)
        clear_stored_location
      end
    end
    

    Then you can use something like redirect_back_or_to dashboard_path in your login action.

    For some controllers/actions, I don't want to store the location (e.g. the login page). For those I just skip the filter:

    class UserController < ApplicationController
      skip_after_filter :store_location
    end
    

    There's also a page in the Devise wiki on doing this: https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in

提交回复
热议问题