The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
What's New in Edge Rails: Nested Resource RESTful URL Builder

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: Nested Resource RESTful URL Builder Posted: Jun 6, 2007 7:11 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: Nested Resource RESTful URL Builder
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

The march to RESTful nirvana continues with a drop dead easy way to build nested resource URLs. Let’s review.

Assuming the following routing configuration:

1
2
3
map.resources :categories do |categories|
  categories.resources :articles
end

This automatically builds a category_url(category) and category_article_url(category, article) helper (among others) that will build the appropriate URLs:

1
2
category_url(@category) #=> /categories/1
category_article_url(@category, @article) #=> /categories/1/article/1

All right, that’s great. But why should we have to spell out these lengthy helper method calls when it’s clear that the first argument is a Category and the second is an Article? Can’t the appropriate helper method be implied by the types of these arguments? Yes – they can, and now they are with the new url_for update. Now you can pass in a collection of models to url_for, in order of nesting (root to child), and get the correct URL:

1
2
url_for(@category) #=> /categories/1
url_for([@category, @article]) #=> /categories/1/article/1

You can always fall back to the named routes provided for you by RESTful routing, but you now have an option to build URLs with an implied resource nesting.

tags: ruby, rubyonrails, REST

Read: What's New in Edge Rails: Nested Resource RESTful URL Builder

Topic: Agile RTP Presents Ken Auer Previous Topic   Next Topic Topic: The plan....

Sponsored Links



Google
  Web Artima.com   

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