Devise and stored_location_for: how do you store the return location?

前端 未结 5 1810
我寻月下人不归
我寻月下人不归 2020-12-25 13:11

I have a page whose path is (e.g.) /premises/92 on which I\'m displaying \"please [log in] or [register] for additional information\" if the user is not logged in, and I wan

5条回答
  •  长发绾君心
    2020-12-25 13:27

    I do like this:

    class ApplicationController < AC::Base
      after_filter :clear_attached_unit # UPDATED
    
      protected
      def clear_attached_unit
        session[:attached_unit_path] = nil unless keep_attached_unit_path?
      end
    
      def keep_attached_unit_path? # UPDATED
        @keep_attached_unit_path   
      end                           
    end
    
    
    class UnitController < ApplicationController
      before_filter :attach_unit, :only => [:show]
    
      protected
      def attach_unit
        session[:attached_unit_path] = request.url if request.get? && !request.xhr?
      end
    end
    
    class SessionsController < Devise::SessionsController
      before_filter :keep_attached_unit_path! # UPDATED
    
      protected
      def keep_attached_unit_path! # UPDATED
        @keep_attached_unit_path = true
      end
    
      def after_sign_in_path_for(resource_or_scope)
        if resource_or_scope.is_a?(User) && session[:attached_unit_path].present?
          session[:attached_unit_path]
        else
          super
        end
      end 
    end
    

    And extract this to module.

提交回复
热议问题