Routes question

Hello,

I'm trying to play with routes but I'm stuck with something ...

The following route allows me to map nice urls to a content :

map.content ':category/:content', :controller => "home", :action => "show"

but this route has a draw back, I can't access anymore to given parts of my app as 'admin/content' or 'admin/category' because these urls match my content route.

So my question is, how can I skip this route when my url begins with 'admin' ?

Thanks in advance.

try putting it lower in the list. routes match the first one found in the list, so if ':category/:content' is above 'admin/content' then it's always going to match and 'admin/content' will never be seen.

Chris

Yes, I know but I've just have this :

   map.home '', :controller => "home"    map.content ':category/:content', :controller => "home", :action => "show"

   # Install the default route as the lowest priority.    map.connect ':controller/:action/:id'

so I can't lower it and I can't find a requirement that do what I want ...

I tried something like :

   map.admin 'admin/:action', :controller => "admin/#{:action}"

but it doesn't work ... and I don't want to create a route for each section (module) of my admin area ...

Any idea ?

you shouldn't have to create a route for each module in your admin area

map.admin 'admin/:action', :controller => 'admin'

should be enough. put it between map.home and map.content routes

so when rails sees anything beginning with admin it will route to the admin controller and whatever is in the :action param will map to a method in the controller

ie

'admin/users' # controller = 'admin', action = 'users' 'admin/content' # controller = 'admin', action = 'content'

Chris

It can't work because 'admin/content' or 'admin/category' are controllers because I'm using this :

class Admin::ContentController < Admin::BaseController

The only way I can find is to write a route for each module, eg:

map.admin_content 'admin/content', : controller => 'admin/content'

It's a bit strange to do that ...

So my routes.rb is like that :

ActionController::Routing::Routes.draw do |map|    map.home '', :controller => "home"

   # Admin routes    map.connect 'admin/category', :controller => 'admin/category'    map.connect 'admin/content', :controller => 'admin/content'    map.connect 'admin/definition', :controller => 'admin/definition'    map.connect 'admin/file', :controller => 'admin/file'    map.connect 'admin/layout', :controller => 'admin/layout'

   # Content route    map.content ':category/:content', :controller => "home", :action => "show"

   # Install the default route as the lowest priority.    map.connect ':controller/:action/:id' end

It's so ugly ...

Any idea ?

unfortunately when using controllers that way, I don't think there is any way to setup routes any differently. perhaps someone else on the list has some additional insight.

one potential problem would be if someone tried to access an admin route that did not exist, such as /admin/whatever'. it would not match any of your defined admin routes and would then pass to ':category/:content' which would be routed to 'home/show'...something to watch for. i think one additional route to catch anything else.

'admin/:missing' might be needed and could be routed to a 404 page or something.

keep in mind that you're not going to be editing routes much. so while you might consider it ugly, it's only in the routes.rb file and not the rest of your code.

you might want to go one extra step and use named routes as that will make the rest of your code a bit prettier.

ie

map.category_admin 'admin/category', :controller => 'admin/category'

then any link to that will be

link_to "Category Admin", category_admin_url, ...

rather than

link_to "Category Admin", :controller => "admin/category"

Nicolas :

So my routes.rb is like that :

ActionController::Routing::Routes.draw do |map|    map.home '', :controller => "home"

   # Admin routes    map.connect 'admin/category', :controller => 'admin/category'    map.connect 'admin/content', :controller => 'admin/content'    map.connect 'admin/definition', :controller => 'admin/definition'    map.connect 'admin/file', :controller => 'admin/file'    map.connect 'admin/layout', :controller => 'admin/layout'

   # Content route    map.content ':category/:content', :controller => "home", :action => "show"

   # Install the default route as the lowest priority.    map.connect ':controller/:action/:id' end

It's so ugly ...

Have you tried :

map.connect 'admin/:controller/:action/:id'

   -- Jean-François.

Yes but it doesn't work.

Ok, thank you for help :slight_smile: