title: Devise: after sign-in redirect back to cancan protected path
body: <p>I am using both Cancan and Devise. I have the following so when cancan errors redirects the page to sign in path:</p>
  
  <pre><code>  check_authorization :unless =&gt; :devise_controller?
    rescue_from CanCan::AccessDenied do |exception|
      redirect_to new_user_session_path, :alert =&gt; exception.message
    end
  </code></pre>
  
  <p>That works well. However, I want to be able to redirect back to that Cancan protected page once user has signed in.</p>
  
  <p>I use the script in the Devise wiki here (<a href="https://github.com/plataformatec/devise/wiki/How-To%3a-redirect-to-a-specific-page-on-successful-sign-in#redirect-back-to-current-page-without-oauth-signin" rel="nofollow">How To: redirect to a specific page on successful sign in</a>).</p>
  
  <pre><code>  def after_sign_in_path_for(resource)
      sign_in_url = url_for(:action =&gt; 'new', :controller =&gt; 'sessions', :only_path =&gt; false, :protocol =&gt; 'http')
      if request.referer == sign_in_url
        # this path is used
        super
      else
        stored_location_for(resource) || request.referer || root_path
      end
    end
  </code></pre>
  
  <p>But this will always use the <code>super</code> method, which goes to index page. What have I done wrong?</p>