Chronicling my experiences with ruby on rails, web application development/management.

Saturday, November 15, 2008

The tail of two admin panels

What are the most popular ways of handling an admin area in a web application?  Since i develop in rails, my focus will be on rails, however i do love the built in admin panel from django.  Excuse me DHH for I have sinned....

Admin Concept #1 - separate namespace in url (i.e., /admin)

When adding an admin namespace, the following steps should be followed:
  1. change routes
  2. setup new directory structure in app/
  3. change views to reflect new namespace
  4. secure access to the new admin namespace
1. Change your routes to reflect the new namespace.  for instance:
 map.namespace :admin do |admin|
    admin.resources :posts
    admin.resources :categories
    admin.resources :forums
    admin.resources :products
  end
Just for fun, run rake routes in  your app, and look at all of your new lovely routes!

2. Include a new admin directory under your app/controller, app/views and app/helpers.  E.g., app/controller/admin/nameofcontroller.rb.

You will need to change the first line of your code to read: class Admin::YourController < ApplicationController

Any redirects in your controller code must be changed to reflect your new namespace.  simply replace your existing @variable with  [:admin, @your_variable] in the redirect.  This code will redirect to the proper show method.

3.  For any new or edit views, you need to change your form_for to reflect your new namespace as well.  Changing the @variable again to reflect the new [:admin, @variable] should do the trick in most cases.  

Also, your routes have changed, so you will need to change your restful routes in all of your new admin/model views.  Usually you only need to add "admin_" to the existing route.  e.g., posts_path would become admin_posts_path.

Concept #2 - inline administration.  

Allows users to administer their site based on their user Id.  If the user is set to admin in the database, then more options appear on their profile.

That user would then have access to /settings, for instance, because of their admin status.

The Settings model would be set up as usual, i.e., no namespace needed, but a before_filter would be used to allow access only to that admin user.  

Question:  Is there a third concept?  What other ways can be used to allow for an admin "area"? Please post your comments, as I would love to hear your thoughts.

2 comments:

  1. Thanks. I found this helpful. Keep up the good work.

    ReplyDelete
  2. Excellent, I was wondering what the best way of doing this was and you just answered the question.

    Thanks again!

    ReplyDelete