Archive for Ruby on Rails

Ivan Vanderbyl on Rails – Interview

// May 28th, 2009 // Comments Off // General ramblings, Ruby on Rails, Site launches

I was recently interviewed by EngineActive’s Michael Troy for their new blog about everything, including the web development scene in Newcastle. So if you would like to learn a bit more about me, have a read.

How to programmatically keep your job, the Rails way

// January 27th, 2009 // 1 Comment » // General ramblings, Ruby on Rails

Read the following code carefully, I found it in the Rails API docs for ActiveRecord transactions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class BooksController < ActionController::Base
    def create
      Book.transaction do
        book = Book.new(params[:book])
        book.save!
        if today_is_friday?
          # The system must fail on Friday so that our support department
          # won't be out of job. We silently rollback this transaction
          # without telling the user.
          raise ActiveRecord::Rollback, "Call tech support!"
        end
      end
      # ActiveRecord::Rollback is the only exception that won't be passed on
      # by ActiveRecord::Base.transaction, so this line will still be reached
      # even on Friday.
      redirect_to root_url
    end
  end

Exceptional exception handling

// November 25th, 2008 // 3 Comments » // Ruby on Rails, development

For the best part of the last 2 years I had been using the Exception Notification plugin for Rails, however recently I found that it was actually causing some of the errors in the application and not reporting them, so I went in search of an alternative, and to my luck found Exceptional, the exception tracking and managing utility for Rails, which was in BETA release, I have now been using it with all my production applications for nearly 2 months and its doing great.

Exceptional has great integration with Twitter, Lighthouse and Campfire, I would like Unfuddle integration but you can’t have everything can you.

Exceptional also groups exceptions which occur more than once so you don’t get bombarded every 10 seconds when something does go wrong.

Give it a try for free

Displaying Flash’s in Rails easily

// October 31st, 2008 // 2 Comments » // CSS, Design, Ruby on Rails, development

In all my Rails applications I make use of the Flash method for displaying simple messages about system changes to the user, such as “Welcome back Ivan” when they login, or “Your details have been saved successfully” when the user saves their profile.

Rails can handle multiple Flash states in the flash hash, e.g. :notice, :error, :success. But how do you display these messages in a no nonsense fashion? with this simple display_flashes helper method I’ve written.

1
2
3
4
5
6
7
8
9
10
11
12
module ApplicationHelper
 
  def display_flashes
    return if flash.blank?
    flash_type = flash.keys.first.to_sym
 
    return content_tag(:div, :id => "flash_box", :class => "flash-wrapper") do
      content_tag(:div, flash[flash_type], :class => "flash #{flash_type}")
    end
  end
 
end

(more…)

Return of the Beer.new

// July 6th, 2008 // 3 Comments » // General ramblings, Ruby on Rails, Uncategorized

Yes that’s right, last month when I was visiting the Sydney Ruby on Rails monthly meetup I was introduced to the Beer model, not the type of model your thinking about, something more along the lines of:

class Beer < ActiveRecord::Base
  CAT_PISS = 2
  named_scope :drinkable, :conditions => ['quality > ?', CAT_PISS]
  # ...
end
 
Beer.drinkable
 
#=> [#<Beer id: 25, name: "Tooheys New", quality: 4, rating: 7.1, best_served: "cold">, 
#<Beer id: 17, name: "Corona", quality: 7, rating: 9.0, best_served: "cold with lime" >]

(Its a Rails thing)