Creating a RESTful admin section in Rails with 2 controllers
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...
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 sectiondownload the new project files to follow along with here.
Ruby on Rails
rails
...oh and as always…please to enjoy…the project
Hey,
Thanks for writing this up, it really helped clear some of my questions up.
- John