Barefoot Development

RESTful Rails Naming Conventions with Nested Controllers

I recently spent some time learning how to name everything in Rails' new RESTful map.resources world, given that I like to group my controllers into subdirectories. For typical content managed sites, I keep the main site in a directory /site, and the content management tools in a directory /admin.

So, how do you get your syntactically-sugar-coated paths when you have a controller in a directory like this:


The map.resources line that worked for me is:

map.resources :users, :controller => 'admin/users', :name_prefix => 'admin_', :path_prefix => '/admin'

Some of the URLs of the resources that are automatically by that line include:

/admin/users: list all users (index action)
/admin/user/1: show one user's details (show action)
/admin/user/new: create a new user (new action)

When you want to use the *_path or *_url shortcuts for these nested controllers, these work:

admin_users_path: renders /admin/users
admin_edit_user_path(@user): renders /admin/user/1;edit

Rails is opinionated, but once you figure out how to agree with its opinion of how things should be named, it makes the rest of the development process a lot easier.

Doug Smith, Senior Developer, Barefoot

Labels: , ,