RAILS 3
Where
Routing gets awesome
Athens Ruby Meetup #4
Nov. 11 2010
Thursday, November 11, 2010
Me
Σάββας Γεωργίου
Software engineer - Ruby on Rails Developer
http://healthleap.com
Thursday, November 11, 2010
Routing in Rails
Each time a request comes to the server, Rails
needs to direct it to some action in some controller
Thurs...
Routing in Rails
Each time a request comes to the server, Rails
needs to direct it to some action in some controller
Thurs...
Routing in Rails
Each time a request comes to the server, Rails
needs to direct it to some action in some controller
"cont...
Routing in Rails
Each time a request comes to the server, Rails
needs to direct it to some action in some controller
"cont...
Routing in Rails
Each time a request comes to the server, Rails
needs to direct it to some action in some controller
"cont...
Routing in Rails
Each time a request comes to the server, Rails
needs to direct it to some action in some controller
"cont...
Routing in Rails
Each time a request comes to the server, Rails
needs to direct it to some action in some controller
"cont...
Routing in Rails
Each time a request comes to the server, Rails
needs to direct it to some action in some controller
"cont...
Routing in Rails
Each time a request comes to the server, Rails
needs to direct it to some action in some controller
"cont...
Resources Routing
Thursday, November 11, 2010
Resources Routing
resources :photos
Thursday, November 11, 2010
Resources Routing
resources :photos
Thursday, November 11, 2010
Resources Routing
resources :photos
HTTP supports 4 verbs. GET, POST, PUT, DELETE
Browsers are dump enough to forget that,...
Resources Routing #2
Thursday, November 11, 2010
Resources Routing #2
new_photo_path()
GET for new
edit_photo_path()
GET for edit
photos_path()
GET for index
POST for crea...
Resources Routing #2
new_photo_path()
GET for new
edit_photo_path()
GET for edit
photos_path()
GET for index
POST for crea...
More Resources Routing - Members
Thursday, November 11, 2010
More Resources Routing - Members
What if we need more RESTful actions for our model?
Thursday, November 11, 2010
More Resources Routing - Members
What if we need more RESTful actions for our model?
resources :photos do
member do
get ‘p...
More Resources Routing - Members
What if we need more RESTful actions for our model?
resources :photos do
member do
get ‘p...
More Resources Routing - Members
What if we need more RESTful actions for our model?
resources :photos do
member do
get ‘p...
More Resources Routing - Collections
Thursday, November 11, 2010
More Resources Routing - Collections
resources :photos do
collection do
get ‘search’
end
end
Thursday, November 11, 2010
More Resources Routing - Collections
resources :photos do
collection do
get ‘search’
end
end
search_photos_path
search_pho...
More Resources Routing - Collections
resources :photos do
collection do
get ‘search’
end
end
search_photos_path
search_pho...
Named routes - Match
Thursday, November 11, 2010
Named routes - Match
For special cases and pretty URLs
Thursday, November 11, 2010
Named routes - Match
For special cases and pretty URLs
match ‘login‘ => ‘user_sessions#new’
match ‘logout‘ => ‘user_sessio...
Named routes - Match
GET /login {:controller=>"user_sessions", :action=>"new"}
GET /logout {:controller=>"user_sessions", ...
Named routes - Match
GET /login {:controller=>"user_sessions", :action=>"new"}
GET /logout {:controller=>"user_sessions", ...
Routes Priority - the 1st WINS
Thursday, November 11, 2010
Routes Priority - the 1st WINS
- Routes are matches matched in top to bottom order as
they appear in the routes file
Thursd...
Routes Priority - the 1st WINS
- Routes are matches matched in top to bottom order as
they appear in the routes file
- The ...
Routes Priority - the 1st WINS
- Routes are matches matched in top to bottom order as
they appear in the routes file
- The ...
Routes Priority - the 1st WINS
- Routes are matches matched in top to bottom order as
they appear in the routes file
- The ...
Routes Priority - the 1st WINS
- Routes are matches matched in top to bottom order as
they appear in the routes file
- The ...
Rails 3 vs Rails 2.3
Thursday, November 11, 2010
Rails 3 vs Rails 2.3
Thursday, November 11, 2010
Rails 3 vs Rails 2.3
The old style map commands still work as before with a
backwards compatibility layer, however this wi...
Rails 3 vs Rails 2.3
The old style map commands still work as before with a
backwards compatibility layer, however this wi...
Thursday, November 11, 2010
Thursday, November 11, 2010
Thursday, November 11, 2010
Thursday, November 11, 2010
Rack it UP
Thursday, November 11, 2010
Rack it UP
In Rails 3 we can have routes directly to Rack
Applications.
Thursday, November 11, 2010
Rack it UP
In Rails 3 we can have routes directly to Rack
Applications.
MyApplication::Application.routes do
match "/home"...
Rack it UP
In Rails 3 we can have routes directly to Rack
Applications.
MyApplication::Application.routes do
match "/home"...
Rack it UP
In Rails 3 we can have routes directly to Rack
Applications.
MyApplication::Application.routes do
match "/home"...
Rack it UP
In Rails 3 we can have routes directly to Rack
Applications.
MyApplication::Application.routes do
match "/home"...
Rack it UP
In Rails 3 we can have routes directly to Rack
Applications.
MyApplication::Application.routes do
match "/home"...
Rack it UP
In Rails 3 we can have routes directly to Rack
Applications.
MyApplication::Application.routes do
match "/home"...
Better Constraints
Thursday, November 11, 2010
Better Constraints
Native support for subdomains
Thursday, November 11, 2010
Better Constraints
Native support for subdomains
Thursday, November 11, 2010
Better Constraints
Native support for subdomains
Regular Expressions in constraints
Thursday, November 11, 2010
Better Constraints
Native support for subdomains
Regular Expressions in constraints
Thursday, November 11, 2010
Better Constraints
Native support for subdomains
Regular Expressions in constraints
Constraints can be
objects & can be
sp...
Better Constraints
Native support for subdomains
Regular Expressions in constraints
Constraints can be
objects & can be
sp...
Thursday, November 11, 2010
Thank you!
http://yehudakatz.com/2009/12/26/the-rails-3-router-rack-it-up/
http://guides.rubyonrails.org/routing.html
http...
Upcoming SlideShare
Loading in …5
×

Routing in Rails 3 - Athens Ruby Meetup #4

1,326 views
1,198 views

Published on

My presentation @ Athens Ruby Meetup #4 that took place in November 11th 2010 in Athens, Greece.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,326
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Routing in Rails 3 - Athens Ruby Meetup #4

  1. 1. RAILS 3 Where Routing gets awesome Athens Ruby Meetup #4 Nov. 11 2010 Thursday, November 11, 2010
  2. 2. Me Σάββας Γεωργίου Software engineer - Ruby on Rails Developer http://healthleap.com Thursday, November 11, 2010
  3. 3. Routing in Rails Each time a request comes to the server, Rails needs to direct it to some action in some controller Thursday, November 11, 2010
  4. 4. Routing in Rails Each time a request comes to the server, Rails needs to direct it to some action in some controller Thursday, November 11, 2010
  5. 5. Routing in Rails Each time a request comes to the server, Rails needs to direct it to some action in some controller "controller"=>"searches" "action"=>"show", "id"=>"allergists", Thursday, November 11, 2010
  6. 6. Routing in Rails Each time a request comes to the server, Rails needs to direct it to some action in some controller "controller"=>"searches" "action"=>"show", "id"=>"allergists", Generates Paths and URLs Thursday, November 11, 2010
  7. 7. Routing in Rails Each time a request comes to the server, Rails needs to direct it to some action in some controller "controller"=>"searches" "action"=>"show", "id"=>"allergists", Generates Paths and URLs - photos_path returns /photos Thursday, November 11, 2010
  8. 8. Routing in Rails Each time a request comes to the server, Rails needs to direct it to some action in some controller "controller"=>"searches" "action"=>"show", "id"=>"allergists", Generates Paths and URLs - photos_path returns /photos - new_photo_path returns /photos/new Thursday, November 11, 2010
  9. 9. Routing in Rails Each time a request comes to the server, Rails needs to direct it to some action in some controller "controller"=>"searches" "action"=>"show", "id"=>"allergists", Generates Paths and URLs - photos_path returns /photos - new_photo_path returns /photos/new - photo_path(id) returns /photos/:id Thursday, November 11, 2010
  10. 10. Routing in Rails Each time a request comes to the server, Rails needs to direct it to some action in some controller "controller"=>"searches" "action"=>"show", "id"=>"allergists", Generates Paths and URLs - photos_path returns /photos - new_photo_path returns /photos/new - photo_path(id) returns /photos/:id - new_photo_url returns http://mydomain:port/photos/new Thursday, November 11, 2010
  11. 11. Routing in Rails Each time a request comes to the server, Rails needs to direct it to some action in some controller "controller"=>"searches" "action"=>"show", "id"=>"allergists", Generates Paths and URLs - photos_path returns /photos - new_photo_path returns /photos/new - photo_path(id) returns /photos/:id - new_photo_url returns http://mydomain:port/photos/new - new_photo_url(id) returns http://mydomain:port/photos/:id Thursday, November 11, 2010
  12. 12. Resources Routing Thursday, November 11, 2010
  13. 13. Resources Routing resources :photos Thursday, November 11, 2010
  14. 14. Resources Routing resources :photos Thursday, November 11, 2010
  15. 15. Resources Routing resources :photos HTTP supports 4 verbs. GET, POST, PUT, DELETE Browsers are dump enough to forget that, but rails can still make it with hidden parameters. Thursday, November 11, 2010
  16. 16. Resources Routing #2 Thursday, November 11, 2010
  17. 17. Resources Routing #2 new_photo_path() GET for new edit_photo_path() GET for edit photos_path() GET for index POST for create photo_path(@photo) GET for show PUT for update DELETE for destroy Thursday, November 11, 2010
  18. 18. Resources Routing #2 new_photo_path() GET for new edit_photo_path() GET for edit photos_path() GET for index POST for create photo_path(@photo) GET for show PUT for update DELETE for destroy We build internet applications over HTTP. We ‘d better use 100% of our protocol. Thursday, November 11, 2010
  19. 19. More Resources Routing - Members Thursday, November 11, 2010
  20. 20. More Resources Routing - Members What if we need more RESTful actions for our model? Thursday, November 11, 2010
  21. 21. More Resources Routing - Members What if we need more RESTful actions for our model? resources :photos do member do get ‘preview’ get ‘print’ put ‘flag’ end end Thursday, November 11, 2010
  22. 22. More Resources Routing - Members What if we need more RESTful actions for our model? resources :photos do member do get ‘preview’ get ‘print’ put ‘flag’ end end preview_photo_path(@photo) print_photo_path(@photo) flag_photo_path(@photo) preview_photo_url(@photo) ..... Available helpers Thursday, November 11, 2010
  23. 23. More Resources Routing - Members What if we need more RESTful actions for our model? resources :photos do member do get ‘preview’ get ‘print’ put ‘flag’ end end preview_photo_path(@photo) print_photo_path(@photo) flag_photo_path(@photo) preview_photo_url(@photo) ..... Available helpers GET /photos/:id/preview {:controller=>"photos", :action=>"preview"} GET /photos/:id/print {:controller=>"photos", :action=>"print"} PUT /photos/:id/flag {:controller=>"photos", :action=>"flag"} Our Application responds to Thursday, November 11, 2010
  24. 24. More Resources Routing - Collections Thursday, November 11, 2010
  25. 25. More Resources Routing - Collections resources :photos do collection do get ‘search’ end end Thursday, November 11, 2010
  26. 26. More Resources Routing - Collections resources :photos do collection do get ‘search’ end end search_photos_path search_photos_url available helpers Thursday, November 11, 2010
  27. 27. More Resources Routing - Collections resources :photos do collection do get ‘search’ end end search_photos_path search_photos_url available helpers GET /photos/search {:controller=>"photos", :action=>"search"} Our Application responds to Thursday, November 11, 2010
  28. 28. Named routes - Match Thursday, November 11, 2010
  29. 29. Named routes - Match For special cases and pretty URLs Thursday, November 11, 2010
  30. 30. Named routes - Match For special cases and pretty URLs match ‘login‘ => ‘user_sessions#new’ match ‘logout‘ => ‘user_sessions#destroy’ I.E. Login action looks better in /login than /user_sessions/new Thursday, November 11, 2010
  31. 31. Named routes - Match GET /login {:controller=>"user_sessions", :action=>"new"} GET /logout {:controller=>"user_sessions", :action=>"destroy"} Our Application responds to For special cases and pretty URLs match ‘login‘ => ‘user_sessions#new’ match ‘logout‘ => ‘user_sessions#destroy’ I.E. Login action looks better in /login than /user_sessions/new Thursday, November 11, 2010
  32. 32. Named routes - Match GET /login {:controller=>"user_sessions", :action=>"new"} GET /logout {:controller=>"user_sessions", :action=>"destroy"} Our Application responds to For special cases and pretty URLs match ‘login‘ => ‘user_sessions#new’ match ‘logout‘ => ‘user_sessions#destroy’ I.E. Login action looks better in /login than /user_sessions/new login_path, login_url, logout_path, logout_url Available Helpers Thursday, November 11, 2010
  33. 33. Routes Priority - the 1st WINS Thursday, November 11, 2010
  34. 34. Routes Priority - the 1st WINS - Routes are matches matched in top to bottom order as they appear in the routes file Thursday, November 11, 2010
  35. 35. Routes Priority - the 1st WINS - Routes are matches matched in top to bottom order as they appear in the routes file - The first route to match an incoming request wins Thursday, November 11, 2010
  36. 36. Routes Priority - the 1st WINS - Routes are matches matched in top to bottom order as they appear in the routes file - The first route to match an incoming request wins - Lower routes may never fire, if a higher route hides them by matching requests Thursday, November 11, 2010
  37. 37. Routes Priority - the 1st WINS - Routes are matches matched in top to bottom order as they appear in the routes file - The first route to match an incoming request wins - Lower routes may never fire, if a higher route hides them by matching requests >> rake routes Thursday, November 11, 2010
  38. 38. Routes Priority - the 1st WINS - Routes are matches matched in top to bottom order as they appear in the routes file - The first route to match an incoming request wins - Lower routes may never fire, if a higher route hides them by matching requests >> rake routes Thursday, November 11, 2010
  39. 39. Rails 3 vs Rails 2.3 Thursday, November 11, 2010
  40. 40. Rails 3 vs Rails 2.3 Thursday, November 11, 2010
  41. 41. Rails 3 vs Rails 2.3 The old style map commands still work as before with a backwards compatibility layer, however this will be removed in the 3.1 release. Thursday, November 11, 2010
  42. 42. Rails 3 vs Rails 2.3 The old style map commands still work as before with a backwards compatibility layer, however this will be removed in the 3.1 release. The routes are attached to your application, which is now its own object and used throughout Railties Thursday, November 11, 2010
  43. 43. Thursday, November 11, 2010
  44. 44. Thursday, November 11, 2010
  45. 45. Thursday, November 11, 2010
  46. 46. Thursday, November 11, 2010
  47. 47. Rack it UP Thursday, November 11, 2010
  48. 48. Rack it UP In Rails 3 we can have routes directly to Rack Applications. Thursday, November 11, 2010
  49. 49. Rack it UP In Rails 3 we can have routes directly to Rack Applications. MyApplication::Application.routes do match "/home", :to => OtherApp end Thursday, November 11, 2010
  50. 50. Rack it UP In Rails 3 we can have routes directly to Rack Applications. MyApplication::Application.routes do match "/home", :to => OtherApp end -- (NOT) -- Thursday, November 11, 2010
  51. 51. Rack it UP In Rails 3 we can have routes directly to Rack Applications. MyApplication::Application.routes do match "/home", :to => OtherApp end -- (NOT) -- In Rails 3 we can have routes ONLY to Rack Applications. Thursday, November 11, 2010
  52. 52. Rack it UP In Rails 3 we can have routes directly to Rack Applications. MyApplication::Application.routes do match "/home", :to => OtherApp end -- (NOT) -- In Rails 3 we can have routes ONLY to Rack Applications. match "/home", :to => “home#show” Thursday, November 11, 2010
  53. 53. Rack it UP In Rails 3 we can have routes directly to Rack Applications. MyApplication::Application.routes do match "/home", :to => OtherApp end -- (NOT) -- In Rails 3 we can have routes ONLY to Rack Applications. match "/home", :to => “home#show” match "/home", :to => HomesController.action(:show) Thursday, November 11, 2010
  54. 54. Rack it UP In Rails 3 we can have routes directly to Rack Applications. MyApplication::Application.routes do match "/home", :to => OtherApp end -- (NOT) -- In Rails 3 we can have routes ONLY to Rack Applications. match "/home", :to => “home#show” match "/home", :to => HomesController.action(:show) HomesController.action(:show) returns a fully valid Rack application pointing at the show action of HomesController. Thursday, November 11, 2010
  55. 55. Better Constraints Thursday, November 11, 2010
  56. 56. Better Constraints Native support for subdomains Thursday, November 11, 2010
  57. 57. Better Constraints Native support for subdomains Thursday, November 11, 2010
  58. 58. Better Constraints Native support for subdomains Regular Expressions in constraints Thursday, November 11, 2010
  59. 59. Better Constraints Native support for subdomains Regular Expressions in constraints Thursday, November 11, 2010
  60. 60. Better Constraints Native support for subdomains Regular Expressions in constraints Constraints can be objects & can be specified in block form Thursday, November 11, 2010
  61. 61. Better Constraints Native support for subdomains Regular Expressions in constraints Constraints can be objects & can be specified in block form Thursday, November 11, 2010
  62. 62. Thursday, November 11, 2010
  63. 63. Thank you! http://yehudakatz.com/2009/12/26/the-rails-3-router-rack-it-up/ http://guides.rubyonrails.org/routing.html http://www.slideshare.net/GreggPollack/rails-3-beautiful- code-3219240 Resources Thursday, November 11, 2010

×