The curse of the spreadsheet!

Posted by Ric Wed, 13 Feb 2008 11:48:00 GMT

I recently wrote about the misuse of spreadsheets on the Swirrl blog.

Spreadsheets are great for some tasks. People love the informality, the flexibility and the instant results: in skilled hands, they can be very powerful. But “when all you have is a hammer, everything looks like a nail”… Read the full article.

no comments | no trackbacks

Swirrl on Rails

Posted by Ric Wed, 06 Feb 2008 17:56:00 GMT

Just a quick announcement about the current rails project I’m working on:

It’s called swirrl and you can read about it here.

no comments | no trackbacks

Handling Errors in Ajax And Rails

Posted by Ric Tue, 05 Feb 2008 16:27:00 GMT

A while ago, I was wondering how I should deal with errors that are encountered in rails controllers, while processing an Ajax request. I played around with some alternatives and eventually came up with one that has been working great for a few months, so I thought I’d share it.

You’ve probably all heard of overriding ‘rescue_action_in_public’ in your ApplicationController to deal with errors. (See simple example below).

def rescue_action_in_public(exception)
  # render the error page.
  render :file => "public/error.html"
end

This works great in regular methods, but what if something happens in an ajax request?

If you take no action, then the controller will fail ‘silently’ (well, some stuff appears in the log, but the user is unaware).

The way I got around this was to add a method to the ApplicationController, similar to that below (irrelevant code stripped out, so apologies if this code has a typo!).

  def perform_ajax_action

    if block_given?
      begin
        if (request.xhr?)
          yield
        else
          raise RuntimeError.new('Not in an ajax request!')
        end
      rescue Exception => error
        logger.error("#{Time.now}: Error performing ajax action: #{error.inspect}")
        logger.error(error.backtrace.join("\n"))
        ajax_aware_redirect_to "/error.html"
      end      
    else
      logger.error("perform_ajax_action called with no block!");
      ajax_aware_redirect_to "/error.html"
    end

  end

(You were probably wondering what this ‘ajax_aware_redirect_to’ business is all about. It’s just another method in the ApplicationController which helps with redirecting during ajax processing. It looks a bit like this: Again, irrelevant stuff removed.)

  def ajax_aware_redirect_to(options = {}, *parameters_for_method_reference)
    if request.xhr?
      render :update do |page| 
        page.redirect_to( options, *parameters_for_method_reference) 
      end
    else
      redirect_to( options, *parameters_for_method_reference)
    end
  end

Now, in order to get this to catch your errors, you need to add an :around_filter to your controller. e.g.

class MyController < ApplicationController

  around_filter :my_around_filter, :only => [:my_action]

  # controller code here....

  def my_around_filter
    perform_ajax_action do
      # do other useful stuff here if you like.
      # in an 'around filter' yield calls the method wrapped in the filter
      yield
    end
  end
end

2 comments | no trackbacks