REST-ful DRY Technique: Make your Controllers Even Smaller

I’m no expert on REST-ful development. But after seeing David’s presentation on ActiveResource, MLY’s article on HABTM, and after looking at Molecule I was inspired to refactor some of my controllers a bit. When I was done, I managed to squeeze the CRUD (Create, Read, Update, Delete) part of my controllers down from around 50-75 lines per controller to around 20-30 lines.


class ApplicationController < ActionController::Base
  ...

  private

  def save_or_update(obj,params=nil)
    begin
      ((params and !obj.new_record?) ? obj.update_attributes(params) : obj.save) or raise errors_for obj
      flash_notice "#{obj.class} saved successfully", :good, :now
      redirect_to :action => "show", :permalink =>obj.permalink
    rescue Exception => e
      flash_notice "There was an error saving the #{obj.class.downcase} #{e}", :bad, :now
    end
  end

  def errors_f