P
A
B
C
A
S
Thoughts, links, pictures on music, food, wine, film, tech etc.

2009-08-05

I 'm excited about this:

Passing controller arguments to to_xml - coming in Rails 3!

What if, in Rails, when building an XML API you wanted to output something like this:

<posts>
  <post>
    <id>1</id>
    <url>http://www.pabcas.com/posts/1</url>
  </post>
</posts>

As of Rails 2.3, there's not really a neat way to do this, using ActiveRecord's to_xml serialization. I needed this functionality today and stumbled across a patch that's been accepted for Rails 3. It will allow something like:

wants.xml { render :xml => @posts.to_xml(:procs => [Proc.new {|options,post| post_url(post) }]) }

Delicious! Bring on Rails 3!

Check out the Lighthouse ticket and the patch.


 
Made by Paul Campbell. paul@rushedsunlight.com. Twitter. Github.