Topic: Is this possible to do in routes?

I have a bunch of admin controllers with names like admin_news, admin_tag, admin_user etc.

I'd like all these admin controllers to be accessible through urls like 'admin/user/list'

What i'd like to do in the routes file is the equivalent of the following:

map.connect 'admin/(*adminsuffix)/:action/:id', :controller=>'admin_'+adminsuffix

Does anyone know of a way to achieve something like this?

Thanks in advance.

Re: Is this possible to do in routes?

You schould have done it by creating modularized controllers:
Admin::NewsController, etc.

so they would be stored in

and accessible from URL like

I dont know if it is possible do it with routes

Re: Is this possible to do in routes?

hi dr_bonzo: that is my current setup.

but i'm investigating whether there is a reasonable way of doing this without namespaced controllers since they seem to give problems in other areas of rails based on the comments here.

mly wrote:


Last edited by cbit (2006-12-03 06:45:14)

Re: Is this possible to do in routes?

Path prefixes will probably work, but I encourage you to consider restructuring the application. It is becoming more and more common to merge the admin controllers with the public controllers, and this is one concept that is at the heart of REST. Instead of splitting up controllers based on how they are accessed, split up controllers based on their funcitonality.

If you read the first article you linked to again, they are now making every controller handle both the admin and public side of the site. This works very well if both the admin and public sides of your site have similar functionality and views. If you want the two sides of your site to be completely different, than this may not work well for you - and that's okay too.

In your case though, it looks like you have an admin controller managing three models: tags, users, and news (might want to rename this to something which can be pluralized). Chances are, if you are following a RESTful design, you have publlic controllers which manage those same models. Try removing the admin controllers completely and moving that functionality into the public controllers. Of course make sure the admin functionality (such as deleting models) has authentication so it can only be accessed by the admin.

Railscasts - Free Ruby on Rails Screencasts

Re: Is this possible to do in routes?

Thanks for the reply, thats helpful. To check my understanding: would a RESTful approach include then, for instance,  having just one method in one controller which was responsible for both showing a record detail page to admin users, and showing the detail page of that same model to public users too? Perhaps the method would use a switch which checked whether a user was logged in to determine which template to serve(?)

Last edited by cbit (2006-12-03 20:26:59)

Re: Is this possible to do in routes?

Yeah, that sounds like a decent way to do it. However, if the views are similar enough, I prefer to keep it in the same template with if conditions in that view to show the admin operations to the proper users.

Railscasts - Free Ruby on Rails Screencasts

Re: Is this possible to do in routes?

When you're going with that approach, what's the best way to use a different layout based on the user's role?  So if I have a controller called "products" and a view called "products/show," how do I get "products/show" to display within the admin layout if the user is an admin or the customer layout if the user is a customer?

Re: Is this possible to do in routes?

You can pass a symbol to the layout method which will allow you to dynamically specify the layout through a method.

class ProductsController < ApplicationController
  layout :user_layout
  def user_layout
    current_user.admin? ? 'admin_layout' : 'customer_layout'

Railscasts - Free Ruby on Rails Screencasts

Posts [ 8 ]

Pages 1