fallenrogue.com

Creating a RESTful admin section in Rails with 2 controllers

required reading: Part 1 in this series on a RESTful admin section you can also download the new project files to follow along with here.

The previous example used 1 controller with routing to do most of the magic. But what if we have a need for 2 different users to perform 2 different actions on the same results set. The model is the same but how they act upon the model is different. What out…an excuse is coming!

I’d like to state for the record, that I’m not sure that this is the best way to go about doing this in most cases. I believe the technique outlined in part one is more DRY and appropriate to the Ruby on Rails paradigm. That said, I can see how if the controllers did get out of controller with logic that you may need to separate them into 2 to better manage each asset. Just because I haven’t found a need for this I can believe that it does exist therefore, this is how I believe you can do it pretty easily. Enjoy crap, onto to code…

First, let’s create a brand new resource and run the schema…
script/generate scaffold_resource widget name:string is_active:integer
rake db:migrate


Now, using my current version of rails you cannot call the following…
script/generate scaffold_resource 'admin/widget'


Which would be nice to give you all the stuff you need. All this will do is give you the views but not the new controller you’re looking to create. Either way, you don’t want a new model anyway, so just call this guy instead…

script/generate controller 'admin/widget'


At this time you’ve got most of the guts of what you want. You may want to copy the views from the generated widget view to the new admin/widget view so that you can get right to seeing this in action. You may also want to save more time by copying the contents of the widget controller to the admin/widgets controller (except the first line… you need that.) Now, in the admin/widgets controller tell it to use the admin layout
layout 'admin'

and in the standard widgets controller, tell the index method to only search for active widgets…
def index
    @widgets = Widget.find(:all, :conditions=>'is_active=1')

    respond_to do |format|
      format.html # index.rhtml
      format.xml  { render :xml => @widgets.to_xml }
    end
  end

Finally, make the routes correct, Here is my entire routes file…
  map.resource :admin do |admin| 
    admin.resources :products, :name_prefix => 'admin_'
    admin.resources :widgets, :name_prefix => 'admin_', :controller=> 'admin/widgets'
  end 
  map.resources :products
  map.resources :widgets


Ok, at this point, I added a checkbox field to the admin views and removed it from the standard widget views. This allows admins to work on the exact same resource as regular users but in limited fashion. Widgets maybe a poor example, though. A better one may be something like a User model that exposes certain properties to the user that they may edit, update or delete while giving site administrators more view (or less) over the same model. While, in most cases, it would make more sense to keep those assets in one controller, I could easily see where if you’re trying to manage 2 sets of CRUD operations in one controller then that’s defeating the entire purpose of the RESTful controller altogether.

Anyway, I hope that this example starts to shed some light into ways you can embrace RESTful interfaces in your applications to make life just a little better for you and your users. Enjoy and happy coding!

...oh and as always…please to enjoy…the project

articleStats

Here are some silly little facts about this Creating a RESTful admin section in Rails with 2 controllers...

It was written by about 1 year ago.
It has 4188 letters in it.
It has 627 words in it.
It has a total of 10 comments in all.
So far Enrico Teotti has the last word!

article Links

These are the links that appear in this article. They probably don't make sense out of context... but just in case. :)

Part 1 in this series on a RESTful admin section
download the new project files to follow along with here.
Ruby on Rails
rails
...oh and as always…please to enjoy…the project
 

The other stuff...

What the kids are saying...

about 1 year ago john said...

Hey,

Thanks for writing this up, it really helped clear some of my questions up.
- John

about 1 year ago fallenrogue said...

no problem man, I'm glad it helped!

about 1 year ago Sandro said...

In this example you created two controllers (widget and admin/widget), I would ask you if there is a way to use the same controller for both the roles...

Is this choice more DRY ? or just more confusing ?

Thank you

about 1 year ago fallenrogue said...

Yup. That was part one of this series.

http://www.fallenrogue.com/articles/178-Creating-a-RESTful-admin-section-in-Rails

I'm of the mind that a single resource can be used for these types of needs in almost all cases so if you need 2 controllers becuase they serve COMPLETELY different needs on a single model, then this will get you there. :)

about 1 year ago Sandro said...

Wow... so, you answered to all my doubts :) Thank you ^_^

about 1 year ago linoj said...

nice job. I fell into this need almost as soon as I started working with RESTful. I have many situations where the end user has (some) CRUD actions, and the admin has them too but access more fields and different view templates. So two controllers is cleaner and makes more sense to me.

about 1 year ago Remi said...

Great 2-part tutorial. This will definitely help me build more RESTful apps, yay for REST :)

about 1 year ago Remi said...

Great 2-part tutorial. This will definitely help me build more RESTful apps, yay for REST :)

about 1 year ago InMan said...

Its pretty old article, but still needed to say Thanks. That really helped. Usually i use like "if logged_in?" etc, but sometimes admin section is needed too.

3 months ago Enrico Teotti said...

intresting article

Leave a comment
*name:
*email: (never sold or published.)
url :

©2000-2008 fallenrogue.com | Some Rights reserved.