The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
What's New in Edge Rails: render Now 70% More Betterer

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
rwdaigle

Posts: 312
Nickname: rwdaigle
Registered: Feb, 2003

Ryan is a passionate ruby developer with a strong Java background.
What's New in Edge Rails: render Now 70% More Betterer Posted: Apr 25, 2007 8:24 AM
Reply to this message Reply

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.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by rwdaigle
Latest Posts From Ryan's Scraps

Advertisement

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 render now 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:

1
2
3
4
5
@contact = Contact.create(params[:contact])
respond_to do |wants|
  wants.xml { render :xml => @contact.to_xml, :status => :created,
                     :location => contact_url(@contact) }
end

Compare this to what you had to do before, which was manually set the location header prior to calling render:


response.headers["Location"] = contact_url(@contact)

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:


render :xml => @contact, :status => :created, :location => contact_url(@contact)

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.

tags: ruby, rubyonrails, REST

Read: What's New in Edge Rails: render Now 70% More Betterer

Topic: Como no escribir un articulo para un blog: ��JSP ja muerto! Previous Topic   Next Topic Topic: API unickifying with JRuby

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use