63
RAILS 3 Where Routing gets awesome Athens Ruby Meetup #4 Nov. 11 2010 Thursday, November 11, 2010

Routing in Rails 3 - Athens Ruby Meetup #4

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Routing in Rails 3 - Athens Ruby Meetup #4

RAILS 3Where

Routing gets awesome

Athens Ruby Meetup #4Nov. 11 2010

Thursday, November 11, 2010

Page 2: Routing in Rails 3 - Athens Ruby Meetup #4

Me

Σάββας ΓεωργίουSoftware engineer - Ruby on Rails Developer

http://healthleap.com

Thursday, November 11, 2010

Page 3: Routing in Rails 3 - Athens Ruby Meetup #4

Routing in RailsEach time a request comes to the server, Rails needs to direct it to some action in some controller

Thursday, November 11, 2010

Page 4: Routing in Rails 3 - Athens Ruby Meetup #4

Routing in RailsEach time a request comes to the server, Rails needs to direct it to some action in some controller

Thursday, November 11, 2010

Page 5: Routing in Rails 3 - Athens Ruby Meetup #4

Routing in RailsEach 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

Page 6: Routing in Rails 3 - Athens Ruby Meetup #4

Routing in RailsEach 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

Page 7: Routing in Rails 3 - Athens Ruby Meetup #4

Routing in RailsEach 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

Page 8: Routing in Rails 3 - Athens Ruby Meetup #4

Routing in RailsEach 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

Page 9: Routing in Rails 3 - Athens Ruby Meetup #4

Routing in RailsEach 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

Page 10: Routing in Rails 3 - Athens Ruby Meetup #4

Routing in RailsEach 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

Page 11: Routing in Rails 3 - Athens Ruby Meetup #4

Routing in RailsEach 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

Page 12: Routing in Rails 3 - Athens Ruby Meetup #4

Resources Routing

Thursday, November 11, 2010

Page 13: Routing in Rails 3 - Athens Ruby Meetup #4

Resources Routingresources :photos

Thursday, November 11, 2010

Page 14: Routing in Rails 3 - Athens Ruby Meetup #4

Resources Routingresources :photos

Thursday, November 11, 2010

Page 15: Routing in Rails 3 - Athens Ruby Meetup #4

Resources Routingresources :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

Page 16: Routing in Rails 3 - Athens Ruby Meetup #4

Resources Routing #2

Thursday, November 11, 2010

Page 17: Routing in Rails 3 - Athens Ruby Meetup #4

Resources Routing #2new_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

Page 18: Routing in Rails 3 - Athens Ruby Meetup #4

Resources Routing #2new_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

Page 19: Routing in Rails 3 - Athens Ruby Meetup #4

More Resources Routing - Members

Thursday, November 11, 2010

Page 20: Routing in Rails 3 - Athens Ruby Meetup #4

More Resources Routing - Members

What if we need more RESTful actions for our model?

Thursday, November 11, 2010

Page 21: Routing in Rails 3 - Athens Ruby Meetup #4

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’ endend

Thursday, November 11, 2010

Page 22: Routing in Rails 3 - Athens Ruby Meetup #4

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’ endend

preview_photo_path(@photo)print_photo_path(@photo)flag_photo_path(@photo)preview_photo_url(@photo)

.....

Available helpers

Thursday, November 11, 2010

Page 23: Routing in Rails 3 - Athens Ruby Meetup #4

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’ endend

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

Page 24: Routing in Rails 3 - Athens Ruby Meetup #4

More Resources Routing - Collections

Thursday, November 11, 2010

Page 25: Routing in Rails 3 - Athens Ruby Meetup #4

More Resources Routing - Collections

resources :photos do collection do get ‘search’ endend

Thursday, November 11, 2010

Page 26: Routing in Rails 3 - Athens Ruby Meetup #4

More Resources Routing - Collections

resources :photos do collection do get ‘search’ endend

search_photos_pathsearch_photos_url

available helpers

Thursday, November 11, 2010

Page 27: Routing in Rails 3 - Athens Ruby Meetup #4

More Resources Routing - Collections

resources :photos do collection do get ‘search’ endend

search_photos_pathsearch_photos_url

available helpers

GET /photos/search {:controller=>"photos", :action=>"search"}

Our Application responds to

Thursday, November 11, 2010

Page 28: Routing in Rails 3 - Athens Ruby Meetup #4

Named routes - Match

Thursday, November 11, 2010

Page 29: Routing in Rails 3 - Athens Ruby Meetup #4

Named routes - MatchFor special cases and pretty URLs

Thursday, November 11, 2010

Page 30: Routing in Rails 3 - Athens Ruby Meetup #4

Named routes - MatchFor 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

Page 31: Routing in Rails 3 - Athens Ruby Meetup #4

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

Page 32: Routing in Rails 3 - Athens Ruby Meetup #4

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

Page 33: Routing in Rails 3 - Athens Ruby Meetup #4

Routes Priority - the 1st WINS

Thursday, November 11, 2010

Page 34: Routing in Rails 3 - Athens Ruby Meetup #4

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

Page 35: Routing in Rails 3 - Athens Ruby Meetup #4

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

Page 36: Routing in Rails 3 - Athens Ruby Meetup #4

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

Page 37: Routing in Rails 3 - Athens Ruby Meetup #4

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

Page 38: Routing in Rails 3 - Athens Ruby Meetup #4

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

Page 39: Routing in Rails 3 - Athens Ruby Meetup #4

Rails 3 vs Rails 2.3

Thursday, November 11, 2010

Page 40: Routing in Rails 3 - Athens Ruby Meetup #4

Rails 3 vs Rails 2.3

Thursday, November 11, 2010

Page 41: Routing in Rails 3 - Athens Ruby Meetup #4

Rails 3 vs Rails 2.3The 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

Page 42: Routing in Rails 3 - Athens Ruby Meetup #4

Rails 3 vs Rails 2.3The 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

Page 43: Routing in Rails 3 - Athens Ruby Meetup #4

Thursday, November 11, 2010

Page 44: Routing in Rails 3 - Athens Ruby Meetup #4

Thursday, November 11, 2010

Page 45: Routing in Rails 3 - Athens Ruby Meetup #4

Thursday, November 11, 2010

Page 46: Routing in Rails 3 - Athens Ruby Meetup #4

Thursday, November 11, 2010

Page 47: Routing in Rails 3 - Athens Ruby Meetup #4

Rack it UP

Thursday, November 11, 2010

Page 48: Routing in Rails 3 - Athens Ruby Meetup #4

Rack it UPIn Rails 3 we can have routes directly to Rack Applications.

Thursday, November 11, 2010

Page 49: Routing in Rails 3 - Athens Ruby Meetup #4

Rack it UPIn Rails 3 we can have routes directly to Rack Applications.MyApplication::Application.routes do match "/home", :to => OtherAppend

Thursday, November 11, 2010

Page 50: Routing in Rails 3 - Athens Ruby Meetup #4

Rack it UPIn Rails 3 we can have routes directly to Rack Applications.MyApplication::Application.routes do match "/home", :to => OtherAppend

-- (NOT) --

Thursday, November 11, 2010

Page 51: Routing in Rails 3 - Athens Ruby Meetup #4

Rack it UPIn Rails 3 we can have routes directly to Rack Applications.MyApplication::Application.routes do match "/home", :to => OtherAppend

-- (NOT) --

In Rails 3 we can have routes ONLY to Rack Applications.

Thursday, November 11, 2010

Page 52: Routing in Rails 3 - Athens Ruby Meetup #4

Rack it UPIn Rails 3 we can have routes directly to Rack Applications.MyApplication::Application.routes do match "/home", :to => OtherAppend

-- (NOT) --

In Rails 3 we can have routes ONLY to Rack Applications.

match "/home", :to => “home#show”

Thursday, November 11, 2010

Page 53: Routing in Rails 3 - Athens Ruby Meetup #4

Rack it UPIn Rails 3 we can have routes directly to Rack Applications.MyApplication::Application.routes do match "/home", :to => OtherAppend

-- (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

Page 54: Routing in Rails 3 - Athens Ruby Meetup #4

Rack it UPIn Rails 3 we can have routes directly to Rack Applications.MyApplication::Application.routes do match "/home", :to => OtherAppend

-- (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

Page 55: Routing in Rails 3 - Athens Ruby Meetup #4

Better Constraints

Thursday, November 11, 2010

Page 56: Routing in Rails 3 - Athens Ruby Meetup #4

Better ConstraintsNative support for subdomains

Thursday, November 11, 2010

Page 57: Routing in Rails 3 - Athens Ruby Meetup #4

Better ConstraintsNative support for subdomains

Thursday, November 11, 2010

Page 58: Routing in Rails 3 - Athens Ruby Meetup #4

Better ConstraintsNative support for subdomains

Regular Expressions in constraints

Thursday, November 11, 2010

Page 59: Routing in Rails 3 - Athens Ruby Meetup #4

Better ConstraintsNative support for subdomains

Regular Expressions in constraints

Thursday, November 11, 2010

Page 60: Routing in Rails 3 - Athens Ruby Meetup #4

Better ConstraintsNative support for subdomains

Regular Expressions in constraints

Constraints can be objects & can be specified in block form

Thursday, November 11, 2010

Page 61: Routing in Rails 3 - Athens Ruby Meetup #4

Better ConstraintsNative support for subdomains

Regular Expressions in constraints

Constraints can be objects & can be specified in block form

Thursday, November 11, 2010

Page 62: Routing in Rails 3 - Athens Ruby Meetup #4

Thursday, November 11, 2010