Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Calling mount and resource inside the router makes mount stop working

#4874

Example:

DeviseMountError::Application.routes.draw do |map|
  class MySinatraBlogApp < Sinatra::Base
    get "/archives" do
      "my old posts"
    end
  end
  mount MySinatraBlogApp, :at => '/blog'

  resource :user
end

If we access "/blog/archives" it does not work unless we comment the resource :user line. Reproducible with Rails 3 beta 4.

Reported by José Valim · June 16th, 2010 @ 05:37 PM

State: resolved
Milestone: 3.0.2
Assigned to: Andrew White Andrew White
Importance: High

Activity

  1. José Valim
    José Valim
    • Assigned user changed from José Valim to josh

    Hey Josh, could you please take a look at this issue? I pushed a failing test here:

    http://github.com/rails/rails/commit/7ba756281ac193e53cabba3ada75f1...

    I've tried to debug it but could not find the reason, thanks!

    June 17th, 2010 @ 12:51 PM

  2. Damien MATHIEU
  3. Andrew White
    Andrew White

    This is a weird one - the resource call can be before or after the mount and it works if the resource call is nested inside a resources call but not inside a namespace. I've tried renaming the resource method but that didn't work. Inspecting the scope before and after the resource call shows a bunch of nil values in the scope but they're exactly the same as a resources call.

    If you turn off all the routes using :only => [], then it works. You can also make a resources call generate the error by doing :only => [:show, :new, :create]. I think the error must be in rack-mount somewhere.

    June 17th, 2010 @ 03:10 PM

  4. josh
    josh

    Works for me.

    Make sure you're using Sinatra 1.x, needs to include this commit http://github.com/sinatra/sinatra/commit/f9a792396ca6172e983b7eb4d7...

    June 22nd, 2010 @ 05:37 PM

  5. Andrew White
    Andrew White

    Still getting the problem here when using Sinatra 1.0. It's triggered by defining a singleton resource at the top level. If you nest the resource then it works. However namespacing the resource doesn't fix it.

    I followed the call down into Rack::Mount with ruby-debug but didn't follow it any further. I'll do some more investigation tonight.

    June 22nd, 2010 @ 06:56 PM

  6. Andrew White
    Andrew White

    It looks as though the problem is somewhere in Rack::Mount::Analysis::Splitting.

    This is what I get for @recognition_key_analyzer.possible_keys when using resource:

    [
      {path_info[0]=>"/blog"}, 
      {:request_method=>/POST/, path_info[0]=>"/user"}, 
      {:request_method=>/GET/, path_info[0]=>"/user/new"}, 
      {:request_method=>/GET/, path_info[0]=>"/user"}, 
      {:request_method=>/PUT/, path_info[0]=>"/user"}, 
      {:request_method=>/DELETE/, path_info[0]=>"/user"}, 
      {:request_method=>/GET/, path_info[0]=>"/user/edit"}, 
      {path_info[0]=>"/rails/info/properties", path_info[1]=>"\000"}
    ]
    

    And this is what I get for @recognition_key_analyzer.possible_keys when using resources:

    [
      {path_info[0]=>"blog"}, 
      {:request_method=>/GET/, path_info[0]=>"users"}, 
      {:request_method=>/POST/, path_info[0]=>"users"}, 
      {:request_method=>/GET/, path_info[0]=>"users", path_info[1]=>"new"}, 
      {:request_method=>/GET/, path_info[0]=>"users", path_info[1]=>/\A([^\/.?]+)\Z/}, 
      {:request_method=>/PUT/, path_info[0]=>"users", path_info[1]=>/\A([^\/.?]+)\Z/}, 
      {:request_method=>/DELETE/, path_info[0]=>"users", path_info[1]=>/\A([^\/.?]+)\Z/}, 
      {:request_method=>/GET/, path_info[0]=>"users", path_info[1]=>/\A([^\/.?]+)\Z/, path_info[2]=>"edit"}, 
      {path_info[0]=>"rails", path_info[1]=>"info"}, path_info[2]=>"properties", path_info[3]=>"\000"
    ]
    

    I'm not sure whether it's significant, but it looks like it might be.

    June 23rd, 2010 @ 11:24 AM

  7. Andrew White
    Andrew White

    Josh, I think I've identified the problem. When you have a small number of routes then '/' may be dropped from the separator pattern because it doesn't appear enough to put it in the upper quartile. Put in a few more routes and it then starts working. Shouldn't '/' always be in the separator pattern?

    June 23rd, 2010 @ 12:06 PM

  8. DHH
    DHH
    • Milestone cleared.

    June 25th, 2010 @ 12:03 AM

  9. José Valim
    José Valim
    • Milestone cleared.
    • State changed from open to resolved
    • Assigned user changed from josh to Andrew White

    Fixed with the commit above applied to rack-mount.

    June 26th, 2010 @ 12:30 AM

  10. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to High

    October 15th, 2010 @ 11:01 PM