colmtuite
colmtuite

Reputation: 4491

Rails Nested Resources with path set to nil

When I order my resources like below, /websitename works but /username doesn't.

resources :websites, :path => '', :only => [:create, :show] do
    resources :pages, :only => [:create, :show, :edit]
end

resources :users, :path => '', :only => [:create, :show, :index]

When I reverse the order like below, /username works but /websitename doesn't.

resources :users, :path => '', :only => [:create, :show, :index]

resources :websites, :path => '', :only => [:create, :show] do
    resources :pages, :only => [:create, :show, :edit]
end

It's like one is cancelling the other out or something. Is there some way around this? Some way to encapsulate them or something? I've no idea what I'm doing...

Upvotes: 0

Views: 456

Answers (1)

Mark Swardstrom
Mark Swardstrom

Reputation: 18070

You can only have one top level resource with no :path in practice. It will always match so nothing after will.

With command line - you can look at rake routes to see the actual url patterns each route is expecting and the precedence order.

And this makes sense, right? You wouldn't know if /georgia was a person or a place.

If your website routes have something special about them (like they all start with 'site') then you could do something like this.

resources :websites, :path => '', constraints: { id: /site+/ }

Put the more restrictive routes first in the routes file.

Upvotes: 1

Related Questions