This project is archived and is in readonly mode.

#5157 ✓resolved
Fouad Mardini

Routing bug when using :constraints

Reported by Fouad Mardini | July 20th, 2010 @ 10:25 AM | in 3.0.2

When matching a request fails due to a constraints proc, the next routes gets params with names as in the failing previous routing rule
Steps to recreate are below

rails new routing_bug -O --edge
cd routing_bug
rails g controller home
rm public/index.html

## edit config/routes.rb to the following
RoutingBug::Application.routes.draw do
  scope "/:country", :constraints => Proc.new { |req| %w[all France Italy Spain].include?(req.symbolized_path_parameters[:country]) } do
    match "/",       :to => "home#index"
    match "/cities", :to => "home#cities"
  end

  match '/:invalid_country(/*other)', :to => redirect { |params, req|
    Rails.logger.warn("~~~~ " + params.inspect)
    # "/all" + params[:other]
    "/all"
  }
end

## Edit app/controllers/home_controller.rb to the following
class HomeController < ApplicationController
  def index
    render :text => params.inspect
  end
  def cities
    render :text => params.inspect
  end
end

$ curl -I localhost:3000/France HTTP/1.1 200 OK
Etag: "e85a20831b64f267c5f41eef6356c95b"
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8
Date: Tue, 20 Jul 2010 09:09:38 GMT
Server: WEBrick/1.3.1 (Ruby/1.8.7/2009-06-12)
X-Runtime: 0.012149
Content-Length: 0
Cache-Control: max-age=0, private, must-revalidate

$ curl -I localhost:3000/Germany HTTP/1.1 301 Moved Permanently
Location: http://localhost:3000/all
Connection: Keep-Alive
Content-Type: text/html
Date: Tue, 20 Jul 2010 09:09:45 GMT
Server: WEBrick/1.3.1 (Ruby/1.8.7/2009-06-12)
X-Runtime: 0.000981
Content-Length: 17

But I would get this in the log
~~~~ {:controller=>"home", :action=>"index", :country=>"Germany"} The behavior I'm trying to achieve is to redirect the users to /all/REST_OF_URL whenever they go to an invalid country
I hacked around it by playing with req.path

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Referenced by

Pages