Using custom mime types

So, you’ve started using Rails’ new mime type support with responds_to, but you were wondering how to add your own custom types? Luckily, Geoffrey Grosenbach (aka topfunky) is there to show you the way. His example involves registering an extension for .png, and generating a custom icon for an order in his shopping cart with some RMagick-fu. His example looks something like this:

Mime::Type.register "image/png", :png

# then in your controller action
def show
  respond_to do |format|
    format.html { }
    format.png { }

Mime::Type.register will add the image/png mime type to the collection of mime types, bind it to the .png extension. It also creates a special Mime::Type instance at Mime::PNG. Check out the great post and the comments for more tips on caching and RMagick.

Note: After some investigation, I’ve found that using Geoff’s :format hack is not required on Rails 1.2 if you make the request with the :format parameter. Using routes like formatted_post_path(@post, :xml) will give you a path like “/posts/1.xml”. Accessing that will write public/posts/1.xml, regardless of what the page_cache_extension is.

A brief introduction to Active Resource

Rick Olson is getting jiggy with Active Resource these days and decided to spread the love with a brief introduction to the newest member of the Rails framework family. He takes you through how to use ARes as a client for the Beast forum in a few simple steps. Get your REST face on and enjoy.

Working with Rails? Tell the world

Working With Rails is an attempt to index all the developers around the world working with Rails. It offers a few ways to browse the index and while the authority and popularity lists are perhaps a tad silly, I love looking at the country list. 3 programmers in Peru, 1 in Kazakhstan, and 3 in Uzbekistan. How cool is that?

Trying to break free from .NET?

Luckymonk escaped their .NET entanglement and found Ruby on Rails, but they still have to live in a Windows world. Now they’re putting on a workshop teaching you how to do the same. It’s in Chicago and it’s almost sold out.

Usually we don’t mention workshops any more (there are so many), but I thought it would be nice to shine some light on one of the lesser published transitions. The one from .NET to Ruby on Rails. We got plenty of stories of how PHP and Java folks are jumping ship, so its great to hear how it’s happening from the Microsoft camp too.

Amazon goes Ruby on Rails

Never thought you’d hear that, eh? But that’s exactly what is. A Rails application sitting on the domain. Adam Selipsky from Amazon Web Services introduces the site with this:

UnSpun helps you to find and create ranked lists by gathering votes from workers on Amazon Mechanical Turk and from the UnSpun community. We show the popular opinion, with no “spin” (hence the name) — along with links to websites with more information about the particular items on the list. If you don’t see the list you are looking for, simply create it and rankings will start populating within a few minutes. 2,294 ranked lists are already on UnSpun, holding 640,107 items, with more coming in all the time.

Congratulations to the AWS team at Amazon!

Coming shortly, we’ll have a bunch of other announcements for high-profile companies going Ruby on Rails for various new projects. Exciting times.

Agile Web Development with Rails, 2nd Edition

How fitting. Just as we’re wrapping up Rails 1.2, the book that covers it all has gone into print. I’m talking about Agile Web Development with Rails, 2nd Edition, of course. The book that has taught the vast majority of all Rails developers the ropes.

Yes, yes, it’ll be available in all its paper glory around December 15th. But you can actually get the final PDF now. Dave Thomas just released the same version as was shipped off to the printers. If you’d like to get access to the information now, but still enjoy reading long sections from paper, there’s the combo pack.

The timing is great and so are the sections on RJS, migrations, resources, respond_to, polymorphic associations, and all the other new-fangled wonders that has been introduced to Rails since the last version of the book. Go get it.

Rails 1.2 RC1: New in Active Support

The following are some of the smaller, but notable features added to Rails 1.2 ActiveSupport since the Rails 1.1 release. (compiled by Joshua Sierles).

Module#unloadable marks constants that require unloading after each request. Example:


Module#alias_attribute clones class attributes, including their getter, setter and query methods. Example:

class Email < ActiveRecord::Base
  alias_attribute :subject, :title

e = Email.find(1)
e.title    # => "Superstars"
e.subject  # => "Superstars"
e.subject? # => true
e.subject = "Megastars"
e.title    # => "Megastars"

Enumerable#sum calculates a sum from the array elements. Examples:

  [1, 2, 3].sum
  payments.sum { |p| p.price * p.tax_rate }

  This replaces: payments.inject(0) { |sum, p| sum + p.price }

Array#to_s(:db) produces a comma-separated list of ids. Example:

Purchase.find(:all, :conditions => "product_id IN (#{shops.products.to_s(:db)})"

Module#alias_method_chain encapsulates the common pattern:

alias_method :foo_without_feature, :foo
alias_method :foo, :foo_with_feature

 With alias_method_chain:

alias_method_chain :foo, :feature

Array#split divides arrays into one or more subarrays by value or block. Examples:

[1, 2, 3, 4, 5].split(3) => [[1, 2], [4, 5]] 
(1..10).to_a.split { |i| i % 3 == 0 }   # => [[1, 2], [4, 5], [7, 8], [10]]

Hash.from_xml(string) creates a hash from an XML string, typecasting its elements if possible. Example:

Hash.from_xml <<-EOT
    <title>This is a note</title>
    <created-at type="date">2004-10-10</created-at>

...would return:

{ :note => { :title => "This is a note", :created_at =>, 10, 10) } }

The Builder package has been upgraded to version 2.0. Changes include:

-- UTF-8 characters in data are now correctly translated to their XML equivalents
-- Attribute values are now escaped by default