#203
Mar 01, 2010

Routing in Rails 3

Rails 3 sports a new routing interface. In this episode I show how to translate the old interface into the new one and show off a few new features.
Download (30.2 MB, 13:31)
alternative download for iPod & Apple TV (19 MB, 13:31)

Resources

rails detour
mate detour
rails g controller info about
rails s
Detour::Application.routes.draw do |map|
  # map.resources :products, :member => { :detailed => :get }
  resources :products do
    get :detailed, :on => :member
  end
  
  # map.resources :forums, :collection => { :sortable => :get, :sort => :put } do |forums|
  #   forums.resources :topics
  # end
  resources :forums do
    collection do
      get :sortable
      put :sort
    end
    resources :topics
  end
  
  # map.root :controller => "home", :action => "index"
  root :to => "home#index"
  
  # map.about "/about", :controller => "info", :action => "about" 
  match "/about(.:format)" => "info#about", :as => :about
  
  match "/:year(/:month(/:day))" => "info#about", :constraints => { :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/ }
  
  match "/secret" => "info#about", :constraints => { :user_agent => /Firefox/ }
  
  constraints :host => /localhost/ do
    match "/secret" => "info#about"
  end
  
  match "/hello" => proc { |env| [200, {}, "Hello Rack!"] }
end
<!-- info/about.html.erb -->
<%= debug params %>

RSS Feed for Episode Comments 16 comments

1. Sleeptillseven Mar 01, 2010 at 00:04

Great and thanks.


2. Tobias Mar 01, 2010 at 00:26

Another great episode! I like Monday mornings ;-)

@Ryan:
One question to the :year, :month, :day variables. Is it possible to set standard values for those that were not provided - or do you have to do this in the controller method itself?

Thanks and keep up the great work!
Tobias


3. Valentin Mar 01, 2010 at 01:32

Thanks for the Screencast! Excellent and helpfull as always. For the next project I'll switch to 3.


4. Pathé Mar 01, 2010 at 02:52

thanks for the screencast, ruby on rails rocks.


5. Danz Mar 01, 2010 at 03:51

Good screencast as usual...
I'm new to rails, and now I can understand about this great framework coz your screencast.
Thanks Ryan...

Since I watch and followed your screencast, I didn't see screencast about Chat Application with Rails. Would you please to make screencast about it (e.g. with XMPP4R or Juggernaut) correct me if I'm wrong)...
Thanks before


6. Peter Mar 01, 2010 at 05:57

The new routing is looking really sexy, like everything I've seen of Rails 3 so far.

It's gonna be another good year for us rubyists ^^


7. Jose Mar 01, 2010 at 07:51

The API is not complete though. Shallow routes are missing, for instance.


8. Ryan Bates Mar 01, 2010 at 09:06

@Tobias, I heard of a :defaults option you can supply for default values, but I could not get this to work. If nothing else it's pretty easy to handle default values on the controller side.

@Jose, right, shallow routes are in the works I believe.


9. Patrick Ma Mar 01, 2010 at 18:28

Great screen-cast, very helpful details on what's to come from Rails 3 in terms of routing. You got a very clear voice, looking forward to more screen-casts.


10. George Mar 02, 2010 at 21:54

Hi Ryan,
Thank you for a great tutorial. Keep it up.

p.s. I noticed what I think might be a typo in the episode description.
"Rails 3 sports" should be "Rails 3 supports".


11. steve Mar 02, 2010 at 22:06

@George: I'll take the liberty of saving Ryan a response. "To sport" is a verb used for precisely this purpose. It means "to wear or display" (something) and is often used as a synonym for "have" when the writer wishes to make the writing feel more active. It also has a positive, energetic connotation, making it well-suited to describing new features of a product, service, or fashion.


12. dstt karte Mar 07, 2010 at 21:26

This is a good screencast.Its good to know about this new routing..You have described very well in this tutorial.I will definitely download it later.


13. mens suit Apr 07, 2010 at 19:14

Nice one. I'll make a download of this code.


14. stuart Apr 20, 2010 at 16:23

Thank you for this good screen cast


15. breilting May 27, 2010 at 20:14

In 130 countries around the world, each piece of Omega


16. parker fountain pen Jun 22, 2010 at 18:07

it is so infomative. thanks so much for your efforts.


17. Gary Jun 27, 2010 at 19:17

I haven't quite figured out why the sexual urge of men and women differ so much. And I never really figured out the whole Venus and Mars thing. I have never figured out why men think with their head and women with their heart.


18. nike jordan Jun 28, 2010 at 18:42

good!


19. nike air force one Jun 28, 2010 at 18:43

nike air force one


20. porno film Jul 04, 2010 at 07:42

greatttttt


21. erotik film Jul 04, 2010 at 07:54

thanksssssssssssssssssssssssssssssssssssssssssssssssssss


22. pornosu Jul 04, 2010 at 08:02

nks for providing such a great resource for us all.


23. anna crystal Jul 13, 2010 at 02:59

This is a good article about the MVC.With the help of the internet,we can share knwmledge with each other.Espcially,with the help of the online stores,we can buy the cheap prom dresses.If you are lucky,you can buy the <a href="http://www.promdressesin.com/dress-between-50-100-37">prom dresses under 100 dollars</a>.Do not hesitate,hurry up to have a look!


24. Kostenlose Software-Downloads Jul 19, 2010 at 01:41

Thank you.I hope I can improve through learning this respect. But overall, it's very nice. Thank you for your share!


25. constipation during pregnancy Jul 19, 2010 at 14:03

the information about the topic is great. how do u get this. i love to read about such knowledge that people share. please keep it up .


26. leis Jul 23, 2010 at 06:27

just i seed your article. i feel so useful . thanks


29. Harry Jul 30, 2010 at 12:35

I wanted to know how to translate the old interface since such a long time. thanks. Now I just need to know how to get my viagra


29. rubbermaid carts Aug 01, 2010 at 11:43

thanks all for the posts and its gud too see so many are participating and commenting on the posts


29. pll fm transmitter Aug 01, 2010 at 19:50

I was just doing some web browsing on my Garmin Phone during my break at my work place, and I came across something I thought was interesting . It linked over to your website so I clicked over. I can’t really find the relevance between your site and the one I came from, but your site good none the less.thanks for sharing thoes informations , It is interesting, i like it!


30. ac adapter Aug 02, 2010 at 02:09

http://u4873.blog.com/

http://dell1300.blog.com/


32. tramadol Aug 07, 2010 at 06:49

Sensational information. I look forward to seeing more.


33. UGG Boots on sale Aug 10, 2010 at 18:49

Gooooooooooooooooooood luck ~~!!


34. Air Max TN Aug 10, 2010 at 22:51

Too beautiful for words!!


35. Nike Free Run+ Men Aug 10, 2010 at 22:52

Brothers do not mistake cutting wood power.


36. Nike Scarpe Aug 10, 2010 at 22:53

Make persistent efforts. Emperor it pays off.


37. Vertu Ascent Aug 10, 2010 at 22:54

God rewards the diligent.


38. Bob Aug 17, 2010 at 16:10

Hi, i'd like to know.. is it possible to use the new rails router in non rails apps?

And if how?

thx :)


39. Rip Blu-ray for Mac Aug 18, 2010 at 01:43

Thanks,it's so good.
suport!


40. wholesale new era caps Aug 20, 2010 at 20:10

The dissertation chapters should be perfectly composed by distinguished thesis service, when people want to present a writing talent. Thus, this is manifestly that you understand the right way to finish a superb issue referring to this topic. Thank you very much for distributing this.


41. converse all star Aug 20, 2010 at 20:50

love converse all star,love yourself.High quality low price.It's fit for you.


42. medyum Aug 22, 2010 at 09:22

The information you provided was very useful. Because of your help, thank you.


43. medyum Aug 22, 2010 at 09:23

Thanks for such a great post and the review, I am totally impressed! Keep stuff like this coming.


44. Wholesale Electronics Aug 25, 2010 at 00:57

Discount Wholesale Electronics, Wholesale Cell Phones, Electronic Gadgets and More from the Best Dropship Wholesaler


45. Thuy Emerich Aug 26, 2010 at 07:16

Good post! I Googled around for this… I located your site!Anyway, would you care if I posted a return link from my blog site to your web site?


46. louis vuitton shoes Aug 26, 2010 at 21:02

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


47. rap Aug 29, 2010 at 08:56

Too beautiful for words!!


48. snow boots Aug 30, 2010 at 20:28

The new routing is looking really sexy, like everything I've seen of Rails 3 so far.


49. nfl hats Aug 31, 2010 at 07:45

hey buddy,this is one of the best posts that I’ve ever seen; you may include some more ideas in the same theme. I’m still waiting for some interesting thoughts from your side in your next post.


50. louis vuitton sunglasses Sep 01, 2010 at 22:19

Good post, I can’t say that I agree with everything that was said, but very good information overall:)


51. lovender90584 Sep 01, 2010 at 23:40

<a href="http://www.theonlinepharma.com" title="Buy Viagra">order online Kamagra</a>

Kamagra contains Sildenafil Citrate, the same active ingredient which is found in prescription Viagra. Kamagra is a cheaper and a generic version of Viagra.
Kamagra pills are an effective solution to aid treat erectile dysfunction, it is a preferred medication as it’s cheaper and just as effective as Viagra. Kamagra oral jelly is simply an alternative solution for those who have trouble swallowing tablets.


52. blu ray ripper Sep 01, 2010 at 23:54

It is really helpful.


53. Band Promotion Sep 02, 2010 at 02:18

Great post

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source