This project is archived and is in readonly mode.

#5052 ✓resolved
Nataniel

namespaced controllers routing - params and url_for

Reported by Nataniel | July 6th, 2010 @ 11:16 AM

routes.rb:

namespace :admin do
  root :to => 'index#index'
  match ":controller(/:action(/:id))"
end
root :to => "site/games#index"
namespace :site do
  root :to => redirect('/')
  match ":controller(/:action(/:id))"
end

When I type in direct URL into the browser, all works fine, like:
localhost:3000/admin/ -> admin/index#index
localhost:3000/admin/reports -> admin/reports#index
localhost:3000/site/ -> site/games#index
localhost:3000/site/security/login -> site/security#login

but when I look in the params, it's not that good:

--- !map:ActiveSupport::HashWithIndifferentAccess 
action: index
controller: reports

(controller should be admin/reports)

Now, because of that - url_for doesn't work as expected:

url_for :controller => 'admin/index', :action => 'index' WORKS FINE
url_for :controller => 'admin/reports', :action => 'index' DOESN'T WORK: No route matches {:controller=>"admin/reports"}
url_for :controller => 'site/security', :action => 'login' DOESN'T WORK: No route matches {:controller=>"site/security", :action=>"login"}

url_for :controller => 'reports', :action => 'index' DOES WORK, but it sends to admin/reports even if called in site/security/login

etc.

If I swap the namespaces declarations, it's all working fine inside "site/" but does not inside "admin/".

Comments and changes to this ticket

  • Nataniel

    Nataniel July 6th, 2010 @ 11:17 AM

    I'm working with edge.

  • Repository

    Repository July 6th, 2010 @ 11:16 PM

    • State changed from “new” to “resolved”

    (from [b802a0d4c72fbd605b92e9f403c9b8765a6e480c]) When a dynamic :controller segment is present in the path add a Regexp constraint that allow matching on multiple path segments.

    Using a namespace block isn't compatible with dynamic routes so we
    raise an ArgumentError if we detect a :module present in the scope.

    [#5052 state:resolved]

    Signed-off-by: José Valim jose.valim@gmail.com
    http://github.com/rails/rails/commit/b802a0d4c72fbd605b92e9f403c9b8...

  • Nataniel

    Nataniel July 7th, 2010 @ 12:49 PM

    • Assigned user set to “José Valim”

    Now, following entry in routes.rb is forbidden:

    namespace :admin do
       match ":controller(/:action(/:id))", :id => /\d+/
    end
    

    So is there any way to use modules with defaults routes? In my app I have few modules, like admin/, site/, api/ and I don't want to mix the controllers together - do I have to declare each controller in routes.rb to achieve it?

  • José Valim

    José Valim July 7th, 2010 @ 12:53 PM

    • Importance changed from “” to “Low”

    Nataniel,

    You should just invoke call without namespace:

    match ":controller(/:action(/:id))", :id => /\d+/
    

    However, if you want to allow just admin controllers, you can dan do something like this:

    match ":controller(/:action(/:id))", :id => /\d+/, :controller => /admin\/.+?/
    
  • Nataniel

    Nataniel July 7th, 2010 @ 01:19 PM

    Thanks a lot, it's all clear and working great now!

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>

People watching this ticket

Referenced by

Pages