Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8148427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:38:39+00:00 2026-06-06T14:38:39+00:00

Hers is my application.rb class ApplicationController < ActionController::Base protect_from_forgery rescue_from CanCan::AccessDenied do |exception| flash[:error]

  • 0

Hers is my application.rb

class ApplicationController < ActionController::Base
  protect_from_forgery

  rescue_from CanCan::AccessDenied do |exception|
    flash[:error] = "You must first login to view this page"
    session[:user_return_to] = request.url
    redirect_to "/users/sign_in"
  end                                                                                                                                                  

end

This will redirect the use to the login page if the AccessDenied is throw and the user is not logged in (“works nicely”), but once logged in it will cause a redirect loop if logged in but not authorized by cancan since the login page will just redirect them back to the user right back via session[:user_return_to] = request.url.

The question is: how do I handle this logic if the user is logged in but not authorized.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:38:41+00:00Added an answer on June 6, 2026 at 2:38 pm

    I added a little condition to make this work.

    class ApplicationController < ActionController::Base
      protect_from_forgery
    
        #Redirects to login for secure resources
        rescue_from CanCan::AccessDenied do |exception|
    
          if user_signed_in?
            flash[:error] = "Not authorized to view this page"
            session[:user_return_to] = nil
            redirect_to root_url
    
          else              
            flash[:error] = "You must first login to view this page"
            session[:user_return_to] = request.url
            redirect_to "/users/sign_in"
          end 
    
        end 
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hers the form code: Imports TechSupportData Public Class frmOpenIncidents Private Sub frmOpenIncidents_Load(ByVal sender As
We have application named Milekeeper , which relays users to send invitations to race
I have an application that searches a database of addresses. The page visitor enters
I'm writing a big webforms application and I'm using the MVP pattern. The application
I am getting this error on python manage.py migrate contacts. The error info does
I have a non document-based Core Data application. There's an NSTreeController that manages a
I'm building a video chat in flash and everything works really well, except for
I have a grouped UITableView and want to select some rows. Hers some handy
I need to prepare Delphi 2007 application for localization. Now, about 80% of what
I came across this question about callbacks in Java. Hers is the running code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.