I hate scaffolding. I truly do, with its repetitive actions and views. I recently found my savior, inherited_resources. This masterpiece allows you to rid of using ActionController directly, and DRY up that wet slop you call a controller. I’d love to give an in depth example, but really, his README is pretty damn intuitive. So, here is a small example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Admin::PagesController < InheritedResources::Base actions :all def update update! do |success, failure| failure.html { redirect_to root_url } end end protected def resource @page ||= end_of_association_chain.find_by_permalink(params[:id]) end def collection @pages ||= end_of_association_chain.paginate(:page => params[:page]) end end |
Normally, this will load the resource automatically, (or collection if I’m hitting #index) but I wanted to find_by_permalink and paginate with will_paginate. To give you an example of redirecting to a different location (or whatever you want, really), I specified the update action, can do any processing, then call update! instead of super, which you pass a block for success/failure. Along with giving me dry controllers, I get url helpers for the resources. resource_url, resource_url(obj), collection_url, and more.
This gives me a hardon. Now, if only Bill Mays was alive to spread the word.
Related posts: