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 7441747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:03:59+00:00 2026-05-29T11:03:59+00:00

I have a constrained route that matches usernames like this: controller :users, :path =>

  • 0

I have a constrained route that matches usernames like this:

controller :users, :path => '/:username', :as => :user, :constrain => { :username => /^_?[a-z]_?(?:[a-z0-9]_?)*$/i } do
   # lots of nested routes go here
end

When I go to write RSpec tests for this (versus using user_id like normal), all the tests are failing because it “can’t find the route” even though it works fine on the server.

describe "for an invalid request" do
  it "should render a 404 if an associated photo is not found" do
    # give it a bad photo id
    xhr :post, :destroy, :id => "999999", :photo_id => "999999", :username => @photo_owner.username
    # not found
    response.status.should == not_found
  end
end

This test was working fine when I was using the user_id in my routes prior to switching to usernames:

resources :users do
  # nested routes
end

and

xhr :post, :destroy, :id => "999999", :photo_id => "999999", :user_id => @photo_owner.id

So what am I doing wrong and what has changed?

My server console shows this which means I should have all of the parameters passed in properly:

Processing by TagsController#destroy as JS
  Parameters: {"constrain"=>{"username"=>/^_?[a-z]_?(?:[a-z0-9]_?)*$/i}, "username"=>"rubynewb", "photo_id"=>"2004-the-title-of-the-photo-here", "id"=>"1797"}
  • 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-29T11:04:00+00:00Added an answer on May 29, 2026 at 11:04 am

    Use :constraints => {...} in your route definition.

    You have one too many parameters being passed…

    "constrain"=>{"username"=>/^_?[a-z]_?(?:[a-z0-9]_?)*$/i}
    

    Rails doesn’t recognize :constrain, therefore it and it’s contents are passed along as a parameter instead of being processed by the Rails router.

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

Sidebar

Related Questions

I have this route that let me build custom url for users like /thisismyname,
I have an asp.net mvc app with a route that allows users to request
I have this route: {controller}/{id}/{action} Because I think it makes more sense from RESTful
I'd like to add a constraint to a route parameter that I have. The
I'm developing in an environment that is severely constrained, but the developers also have
I'm trying to create a rails route for movies (on the root path) that
I'm trying to add a constraint to a controller . I may have this
I have a route (which works): routes.MapRoute( Details, // Route name {controller}/{id}, // URL
i'm working on a app using the route-me project. I'd like to have a
I have a route that is working correctly in the form of the standard:

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.