The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
What's New in Edge Rails: RESTful Routing Updates

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: RESTful Routing Updates Posted: May 6, 2007 5:16 PM
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: RESTful Routing Updates
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

has_many and has_one RESTful Routing

Until the day comes that Rails’ routing can interrogate your domain model and properly translate the existing ActiveRecord associations into RESTful routes, we will have to settle for the same sugary-sweet declaration syntax. Instead of nesting resources in your routing configuration to imply an association, you can now directly specify the routing association with has_one and has_many:


map.resources :posts, :has_one => :author, :has_many => [:comments, :trackbacks]

This gets you what you’re used to seeing as:

1
2
3
4
5
map.resources :posts do |posts|
  posts.resource :author
  posts.resources :comments
  posts.resources :trackbacks
end

You can always drop back to the nested form to specify more detailed options, but for vanilla routes this gets you an intuitive way to specify resource relationships that are directly reflected in your routing.

Auto Routing Name Prefixing

As part of the same update you no longer have to specify a named prefix for nested resources (to avoid conflicts with other named routes) – now named prefixes are assumed for you based on the resource nesting. For instance, this routing:

1
2
3
map.resources :posts do |posts|
  posts.resources :comments
end

now provides a post_comments_url(post_id) helper method. Previously, you had to specify the post_ part of the name via the name_prefix option for your route:

1
2
3
map.resources :posts do |posts|
  posts.resources :comments, :name_prefix => "post_"
end

Now that prefix is assumed for you (an assumption that could potentially break your routing and helper methods). If you don’t want the name_prefix assumed, you’ll need to explicitly set it to nil with :name_prefix => nil.

And don’t forget about the routing namespace update too

tags: ruby, rubyonrails

Read: What's New in Edge Rails: RESTful Routing Updates

Topic: ThinkDRY Interview Previous Topic   Next Topic Topic: Hackety Org

Sponsored Links



Google
  Web Artima.com   

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