This post originated from an RSS feed registered with Ruby Buzz
by rwdaigle.
Original Post: What's New in Edge Rails: render Now 70% More Betterer
Feed Title: Ryan's Scraps
Feed URL: http://feeds.feedburner.com/RyansScraps
Feed Description: Ryan Daigle's various technically inclined rants along w/ the "What's new in Edge Rails" series.
If you’re anything like me, one of the things that really spins your propeller hat is trying to reduce your code into the least amount of lines possible, while still being completely legible. If you’re not like me, well, ya should be.
To that end, calls to rendernow have some new possibilities. The first is the ability to specify the Location header value inline with the render call. This is often used when building RESTful services – when an item is created the URI of that new item needs to be sent back in the Location response header:
And to compress our render line a little bit more, the to_xml conversion on your model is a bit redundant right? I mean, we’re already telling it to render :xml, shouldn’t it know how to convert the model to render as xml? Yeah, it should, and it does now:
See how we lost the unnecessary to_xml there? If your model supports to_xml (which all ActiveRecord models do), render :xml will automatically convert it to xml. Ka-boo-ya, 7 characters gone.