Forum: Ruby on Rails Implementing URLs with more than 3 levels in Rails.

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
B780ee0ee1480454a85df58536702f63?d=identicon&s=25 Alder Green (Guest)
on 2006-05-12 18:07
(Received via mailing list)
Hi There

I started using Rails recently, and I see it's very easy for URLs that
have up to 3 levels:


But what do you do when you need more than 3?

For example, my current project should have an "admin" interface.
Admin has several sub-interfaces, for example a "users" interface. And
the admin/users interface has several actions, e.g. "add", for each
user ID.

Ideally, I'd like to be able to have URLs such as:

But this doesn't seem to work very well with the Rails scheme and
supporting methods, for example "link_to" assuming :action in this
case is "users", :id is "add", and 1, well, doesn't exist :)

(of course it's possible to have the "admin" namespace inferred from a
session, but I'd like a more RESTful approach)

Would appereciate any tips of good ways to do this.
Fb23bc8cd4030c526b0689276b34c8bd?d=identicon&s=25 Bryan Duxbury (bryanduxbury)
on 2006-05-12 18:11

You can do this. The question is, how are you going to lay out the
controllers? Will you have an admin_users controller? Using routes, you
could do:

map.connect 'admin/users/:action/:id', :controller => "admin_users"

and you'd get what you wanted.

I believe there's also a way to put controllers in a subfolder, but I
have no experience with that. Anyone want to comment?
89d967359903c639d31e4cad4569f537?d=identicon&s=25 Charlie Bowman (Guest)
on 2006-05-12 19:03
(Received via mailing list)
Here's an example I use to add the pagination parameters to the url

  # we are going to add the pagination parameters to the url instead of
adding them as parameters
  # this fixes alot of the caching problems
  map.connect 'pragmatic/:action/:id/:page',
  :controller => 'pragmatic',
  :requirements => { :page => /\d+/},
  :page => nil

adding more than three parameters to a url is as simple as creating the

Charlie Bowman
Db59927495b7db991b543d113e1835ff?d=identicon&s=25 Maurice Codik (Guest)
on 2006-05-12 20:40
(Received via mailing list)
This is done using modules.

For example,

create a file, /app/controllers/admin/user_controller.rb

and just make sure that in this file, UserController is inside the Admin

module Admin
  class UserController < ActionController::Base

Routes are then smart enough to figure this out. For example the route,
/:controller/:action, would map the URL '/admin/user/add' to the
Admin::UserController#add method.

Hope this helps,

F639e0cdf2fa098b535d6ec766b2aee1?d=identicon&s=25 Szymon Nowak (g0nzo)
on 2006-05-12 20:54
Like Maurice said you can do "script/generate controller Admin/User" and
you will get user_controller.rb file in app/controllers/admin folder.
Just remember that you can't have admin_controller.rb in app/controllers
folder because it's aleady the name of your group of controllers and
you'll get errors.

If you need some common functionality for all your admin controllers you
could create admin_something.rb controller in app/controllers with
common functionality and inherit all other admin controllers from that.

This way you won't have to mess with routes at all.
B780ee0ee1480454a85df58536702f63?d=identicon&s=25 Alder Green (Guest)
on 2006-05-13 15:51
(Received via mailing list)
On 5/12/06, szymek <> wrote:
> This way you won't have to mess with routes at all.
Indeed, this is the best solution. I do:

ruby script/generate controller admin/users add

And then I have an Admin namespace, where I can put the various admin/
controllers. This is indeed hadnled automatically (and very well) by
Rails' default routing settings. And since an Admin namespace is all I
needed - both for request-handling and code-organization (in a neat,
seperate subdirectory - it's enough to take care of the task I

However, it was good to learn about the available routes.rb methods.
If at any point in the future I need a hierarchy beyond two levels
deep after the controller (all I needed now was the regular 3-level
/:controller/:action/:id inside an Admin namespace), I can simply add
it to routes.rb thus:

ActionController::Routing::Routes.draw do |map|
  map.connect ':controller/:action/:group/:id'

And then things like:

link_to('Add user 21 to the "viewers" group', 'group' => 'viewers', 'id'
=> 21)

would work as expected. (Note: the above link_to call would work well
only inside the 'admin/users' namespace [which is reasonable for this
use-case], the full parameters for getting the same URI from anywhere
in the application is:

link_to('Add user 21 to the "viewers" group', :controller =>
'admin/users', :action => 'add', 'group' => 'viewers', 'id' => 21)

This topic is locked and can not be replied to.