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

  • SEARCH
  • Home
  • 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 3997376
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:23:39+00:00 2026-05-20T07:23:39+00:00

I have a fairly typical require_no_user as a before_filter in one of my controllers.

  • 0

I have a fairly typical require_no_user as a before_filter in one of my controllers. I need to test that a logged in user is redirected by this filter if they try to access any of the controller’s actions.

Is there a sensible way to do this without enumerating all of the controller’s actions in my test case?

I’m trying to avoid:

context 'An authenticated user' do
  setup do
    activate_authlogic
    @user = Factory(:user)
    UserSession.create(@user)
  do

  should 'not be allowed to GET :new' do
    get :new
    assert_redirected_to(root_path)
  end

  should 'not be allowed to POST :create' do
    # same as above
  end

  # Repeat for every controller action
end
  • 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-05-20T07:23:40+00:00Added an answer on May 20, 2026 at 7:23 am

    Not that I’m aware of… though you could make it a bit shorter by packing all the methods and actions into a hash:

    should "be redirected" do
      {
        :get => :new,
        :post => :create,
      }.each do |method, action|
        send(method, action)
        assert_redirected_to(root_path)
      end
    end
    

    Edit: so yeah, this is probably overkill, but here’s another way:

    should "be redirected" do
      ActionController::Routing::Routes.named_routes.routes.each do |name, route|
        if route.requirements[:controller] == @controller.controller_name
          send(route.conditions[:method], route.requirements[:action])
          assert_redirected_to(root_path)
        end
      end
    end
    

    Seems though that if you define multiple :methods in custom routes that it still only “finds” the first, e.g.

    map.resources :foo, :collection => {
      :bar => [:get, :post]
    }
    

    The above route will only be attempted with the GET verb.

    Also if there are other requirements in the URL, such as presence of a record ID, my naive example ignores that requirement. I leave that up to you to hack out 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a django application using mod_python, fairly typical configuration except that media files
For my app, I have a fairly complex set of configuration options that user
OK, I'm stumped. I have a Java tree that looks like a fairly typical
I have a REST API which is fairly typical, except that the id's of
I have a fairly large Excel file. In this file there is a column
I have a fairly standard OLTP normalised database and I have realised that I
I have a fairly complicated installer that I'm writing in Wix that has a
I have a fairly simple python loop that calls a few functions, and writes
Let's say I have a (fairly typical) set of environments: PROD, UAT, QA, DEV
So, I have a fairly typical app with UITableViewController 's inside of an UINavigationController

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.