Presentação-Herminio-CearaOnRails-08-04-09

808 views

Published on

Apresentação sobre rotas no rails na primeira edição do Ceara On Rails de 2009 que ocorreu na data de 08//04/2009

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
808
On SlideShare
0
From Embeds
0
Number of Embeds
162
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Presentação-Herminio-CearaOnRails-08-04-09

  1. 1. URL não mapeada?
  2. 2. RESTful
  3. 3. REST?
  4. 4. Verbos HTTP Ações CRUD
  5. 5. Verbos HTTP Ações CRUD
  6. 6. Verbos HTTP Ações CRUD
  7. 7. Verbos HTTP Ações CRUD
  8. 8. Rails on REST
  9. 9. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.resources :photos end
  10. 10. URLs e Caminhos
  11. 11. URLs e Caminhos Action_controller_url http://www.exemplo.com.br/photos
  12. 12. URLs e Caminhos Action_controller_path http://www.exemplo.com.b /photos
  13. 13. Customizando Recursos
  14. 14. :controller
  15. 15. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.resources :photos, :controller => “images” end
  16. 16. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.resources :photos, :controller => “images” end
  17. 17. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.resources :photos, :controller => “images” end /photos
  18. 18. :as
  19. 19. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.resources :photos, :as => “images” end
  20. 20. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.resources :photos, :as => “images” end
  21. 21. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.resources :photos, :as => “images” end /photos
  22. 22. :as :controller /photos /photos
  23. 23. :as :controller /photos /photos
  24. 24. :as :controller /photos /photos
  25. 25. :path_names
  26. 26. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.resources :photos, :path_names => { :new => ‘novo’, :edit => ‘editar’ } end
  27. 27. # config/enviroment.rb Rails::Initializer.run do |config| config.action_controller.resources_path_names { :new => ‘novo’, :edit => ‘editar’ } end
  28. 28. p://www.example.com.br Raiz do Site ou root(não muda)‫‏‬
  29. 29. / photos / novo p://www.example.com.br controller action Raiz do Site ou root(não muda)‫‏‬
  30. 30. id p://www.example.com.br / photos / 1 / editar Raiz do Site ou root(não muda)‫‏‬ controller action
  31. 31. aninhada
  32. 32. model class Magazine < ActiveRecord::Base # has_many é um relacionamento has_many :ads end class Ad < ActiveRecord::Base # belongs_to é um relacionamento belongs_to :magazine end
  33. 33. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.resources :magazines do |magazine| magazine.resources :ads end end
  34. 34. model + routes
  35. 35. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.resources :magazines, :has_many => :ads end
  36. 36. a url ficaria como?
  37. 37. Assim? id //www.example.com.br / photos?id=1&editar controller action Raiz do Site ou root(não muda)‫‏‬
  38. 38. id //www.example.com.br / photos / 1 / editar controller action Raiz do Site ou root(não muda)‫‏‬
  39. 39. id E quais as vantagens? / photos / 1 / editar controller action
  40. 40. E uma rota aninhada com várias outras?
  41. 41. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.resources :photos do |photo| photo.resource :photographer photo.resources :publications photo.resources :versions end end
  42. 42. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.resources :photos do |photo| photo.resource :photographer photo.resources :publications photo.resources :versions end end Lindo! Não?
  43. 43. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.resources :photos do |photo| photo.resource :photographer photo.resources :publications photo.resources :versions end end Não
  44. 44. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.resources :photos, :has_one => :photographer, :has_many => [ :publications, :versions ] end Lindo!
  45. 45. Uma rota aninhada dentro de outra rota aninhada?
  46. 46. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.resources :publishers do |publisher| publisher.resources :magazines do |magazine| magazine.resources :photos end end end
  47. 47. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.resources :publishers do |publisher| publisher.resources :magazines do |magazine| magazine.resources :photos end end end _path: /publishers/1/magazines/2/photos/3
  48. 48. :shallow
  49. 49. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.resources :publishers, :shallow => true do |publisher| publisher.resources :magazines do |magazine| magazine.resources :photos end end end _path: /publishers/1 _path: /publishers/1/magazines _path: /magazines/2 _path: /magazines/2/photos _path: /photos/3
  50. 50. regular
  51. 51. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.connect ‘:controller/:action/:id’ map.connect ‘:controller/:action/:id.:format’ end
  52. 52. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.connect ‘:controller/:action/:id’ map.connect ‘:controller/:action/:id.:format’ end
  53. 53. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.connect ‘:controller/texto_estatico/:id’ end
  54. 54. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.connect ‘photo/:id’, :controller => ‘photos’, :action => ‘show’ end
  55. 55. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.connect ‘photo/:id’, :controller => ‘photos’, :action => ‘show’, :defaults => { :format => ‘jpg’ } end
  56. 56. nomeada
  57. 57. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.logout ‘/logout’, :controller => ‘sessions’, :action => ‘destroy’ end
  58. 58. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.login ‘/login’, :controller => ‘sessions’, :action => ‘new’, :conditions => { :method => :post } end
  59. 59. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.with_options :controller => ‘photos’ do |photo| photo.delete ‘:id/delete’, :action => ‘delete’ photo.edit ‘:id/edit’, :action => ‘edit’ end end
  60. 60. padrão
  61. 61. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.root :controller => ‘pages’, :action => ‘main’ end
  62. 62. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.root :controller => ‘pages’, :action => ‘main’ end
  63. 63. # config/routes.rb ActionController::Routing::Routes.draw do |map| map.index ‘index’, :controller => ‘pages’, :action => ‘main’ map.root :index end
  64. 64. end
  65. 65. obrigado!

×