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

Posted by ryan
at 6:08 PM on Sunday, May 06, 2007

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

Comments

Leave a response

  1. BJ VicksMay 07, 2007 @ 02:56 PM

    Awesome. Both of these changes are simple and intuitive.

  2. jcJuly 13, 2007 @ 05:13 AM

    How would I go more than 1 level deep?

    /groups/[GROUP_ID]/posts/[POST_ID]/comments/[COMMENT_ID]

  3. EmmanuelSeptember 17, 2007 @ 06:27 PM

    I would like to know the prent/child relationship of resources programatically, i.e, in:

    map.resources :posts do |posts| posts.resources :comments end

    Is there any way i could know that there is a PostsController and a CommentsController and that comments are children of posts? (ie inspecting some collection of ActionController::Base::Resource objects or something like that?) That would be fantastic for automating the scaffolding of controllers.

    Thanks!

  4. TiagoMarch 02, 2010 @ 04:07 PM

    Is it possible to have has_many on a named route? for example:

    map.test ‘test/:test_name’, :controller => :tests do |test| test.other_route ’:other_name’, :controller => :others end

    ?

    I’m trying to figure this out here, but its not working.

  5. sales territory mappingJune 19, 2011 @ 06:01 PM

    Is there a way to take advantage of this without going on Edge rails? I like the idea of continuing RESTful controllers while breaking up the namespace. Even if the routes look a little funky until 2.0, I would be interested in giving it a shot.

  6. geogeJuly 28, 2011 @ 10:44 AM

    2YttWe http://fnYwlOpd2n9t4Vx6A3lbk.com

  7. kostenfrei novostar zockenJuly 28, 2011 @ 04:02 PM

    yeah..true. thanks man :)

  8. StijxymvAugust 04, 2011 @ 07:35 AM
  9. KsrkwsahAugust 05, 2011 @ 09:32 AM
  10. VypedamvAugust 09, 2011 @ 08:54 AM
  11. LbstdasaAugust 11, 2011 @ 04:25 PM
  12. IiallgxnAugust 12, 2011 @ 04:48 AM
  13. NvteedwxAugust 12, 2011 @ 06:19 AM
  14. EkaigevaAugust 12, 2011 @ 11:09 AM
  15. VjvliuoqAugust 12, 2011 @ 04:34 PM
  16. LwdmwllaAugust 14, 2011 @ 06:47 AM
  17. Doria P MustyJanuary 28, 2012 @ 12:40 AM

    I am sure this post has touched all the internet visitors, its really really good post on building up new blog.

  18. TwsouinkFebruary 06, 2012 @ 11:36 AM
  19. tattoo fontsApril 02, 2012 @ 04:04 AM

    Do you mind if I quote a couple of your articles as long as I provide credit and sources back to your weblog? My blog is in the very same niche as yours and my users would truly benefit from a lot of the information you present here. Please let me know if this okay with you. Thanks!

  20. funny dating adviceJune 16, 2012 @ 05:58 AM

    Thank you for sharing your info. I truly appreciate your efforts and I will be waiting for your further write ups thanks once again.

  21. read thisJune 22, 2012 @ 03:23 AM

    I simply could not depart your website before suggesting that I actually loved the usual info an individual provide to your visitors? Is going to be back steadily to check up on new posts

  22. real amaturesSeptember 21, 2012 @ 10:14 PM

    Good post. I learn something totally new and challenging on blogs I stumbleupon everyday. It’s always exciting to read through content from other writers and use something from their sites.

  23. orangeSeptember 27, 2012 @ 10:10 PM

    Visibility is also an important factor. What does your current web site design say about your business? That will certainly be a good help in increasing the bounce rate of the web page. orange Web design pros in Perth can tell just what you need once they look at that website and discuss what is there and what needs to be there. The most important aspect and requirement of todays web site industry are like to follow interactive presentation.

  24. CarlotaOctober 11, 2012 @ 02:08 PM

    you’re really a excellent webmaster. The website loading velocity is amazing. It sort of feels that you are doing any unique trick. In addition, The contents are masterwork. you have done a great task on this matter!

  25. Lytro cameraOctober 14, 2012 @ 06:50 PM

    And as a consequence of better technology today, much more of these camera bags are created to exceed consumer expectations. Before beginning photography, you have to know few details of it.

    Dental Intraoral cameras help patients observe the exact condition of their teeth in order that that can make informed decisions regarding care.

Comment