Routing in Rails 3 - Athens Ruby Meetup #4

Post on 18-May-2015

1,106 views 2 download

Tags:

description

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

transcript

RAILS 3Where

Routing gets awesome

Athens Ruby Meetup #4Nov. 11 2010

Thursday, November 11, 2010

Me

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

http://healthleap.com

Thursday, November 11, 2010

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

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

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

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

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

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

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

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

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

Resources Routing

Thursday, November 11, 2010

Resources Routingresources :photos

Thursday, November 11, 2010

Resources Routingresources :photos

Thursday, November 11, 2010

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

Resources Routing #2

Thursday, November 11, 2010

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

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

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 ‘preview’ get ‘print’ put ‘flag’ endend

Thursday, November 11, 2010

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

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

More Resources Routing - Collections

Thursday, November 11, 2010

More Resources Routing - Collections

resources :photos do collection do get ‘search’ endend

Thursday, November 11, 2010

More Resources Routing - Collections

resources :photos do collection do get ‘search’ endend

search_photos_pathsearch_photos_url

available helpers

Thursday, November 11, 2010

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

Named routes - Match

Thursday, November 11, 2010

Named routes - MatchFor special cases and pretty URLs

Thursday, November 11, 2010

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

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

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

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

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

- The first route to match an incoming request 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

- 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

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

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

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

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

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 UPIn Rails 3 we can have routes directly to Rack Applications.

Thursday, November 11, 2010

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

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

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

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

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

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

Better Constraints

Thursday, November 11, 2010

Better ConstraintsNative support for subdomains

Thursday, November 11, 2010

Better ConstraintsNative support for subdomains

Thursday, November 11, 2010

Better ConstraintsNative support for subdomains

Regular Expressions in constraints

Thursday, November 11, 2010

Better ConstraintsNative support for subdomains

Regular Expressions in constraints

Thursday, November 11, 2010

Better ConstraintsNative support for subdomains

Regular Expressions in constraints

Constraints can be objects & can be specified in block form

Thursday, November 11, 2010

Better ConstraintsNative support for subdomains

Regular Expressions in constraints

Constraints can be objects & can be specified in block form

Thursday, November 11, 2010

Thursday, November 11, 2010