|
| 1 | +# Routing |
| 2 | + |
| 3 | +``` |
| 4 | +get '/patients/:id', to: 'patients#show' |
| 5 | +get 'profile', to: :show |
| 6 | +
|
| 7 | +get '/patients/:id', to: 'patients#show', as: 'patient' |
| 8 | +<%= link_to 'Patient Record', patient_path(@patient) %> |
| 9 | +``` |
| 10 | + |
| 11 | +## Links for resources |
| 12 | +``` |
| 13 | +resources :photos |
| 14 | +
|
| 15 | +GET /photos index display a list of all photos |
| 16 | +GET /photos/new new return an HTML form for creating a new photo |
| 17 | +POST /photos create create a new photo |
| 18 | +GET /photos/:id show display a specific photo |
| 19 | +GET /photos/:id/edit edit return an HTML form for editing a photo |
| 20 | +PATCH/PUT /photos/:id update update a specific photo |
| 21 | +DELETE /photos/:id destroy delete a specific photo |
| 22 | +``` |
| 23 | + |
| 24 | + |
| 25 | +## Links for resource |
| 26 | +``` |
| 27 | +resource :geocoder |
| 28 | +
|
| 29 | +GET /geocoder/new new return an HTML form for creating the geocoder |
| 30 | +POST /geocoder create create the new geocoder |
| 31 | +GET /geocoder show display the one and only geocoder resource |
| 32 | +GET /geocoder/edit edit return an HTML form for editing the geocoder |
| 33 | +PATCH/PUT /geocoder update update the one and only geocoder resource |
| 34 | +DELETE /geocoder destroy delete the geocoder resource |
| 35 | +``` |
| 36 | + |
| 37 | +## Controller Namespaces and Routing |
| 38 | +``` |
| 39 | +namespace :admin do |
| 40 | + resources :posts, :comments |
| 41 | +end |
| 42 | +
|
| 43 | +GET /admin/posts index admin_posts_path |
| 44 | +GET /admin/posts/new new new_admin_post_path |
| 45 | +POST /admin/posts create admin_posts_path |
| 46 | +GET /admin/posts/:id show admin_post_path(:id) |
| 47 | +GET /admin/posts/:id/edit edit edit_admin_post_path(:id) |
| 48 | +PATCH/PUT /admin/posts/:id update admin_post_path(:id) |
| 49 | +DELETE /admin/posts/:id destroy admin_post_path(:id) |
| 50 | +``` |
| 51 | + |
| 52 | +``` |
| 53 | +scope '/admin' do |
| 54 | + resources :posts, :comments |
| 55 | +end |
| 56 | +
|
| 57 | +GET /admin/posts index posts_path |
| 58 | +GET /admin/posts/new new new_post_path |
| 59 | +POST /admin/posts create posts_path |
| 60 | +GET /admin/posts/:id show post_path(:id) |
| 61 | +GET /admin/posts/:id/edit edit edit_post_path(:id) |
| 62 | +PATCH/PUT /admin/posts/:id update post_path(:id) |
| 63 | +DELETE /admin/posts/:id destroy post_path(:id) |
| 64 | +``` |
| 65 | + |
| 66 | +## Nested Resources |
| 67 | +``` |
| 68 | +class Magazine < ActiveRecord::Base |
| 69 | + has_many :ads |
| 70 | +end |
| 71 | + |
| 72 | +class Ad < ActiveRecord::Base |
| 73 | + belongs_to :magazine |
| 74 | +end |
| 75 | +
|
| 76 | +
|
| 77 | +resources :magazines do |
| 78 | + resources :ads |
| 79 | +end |
| 80 | +
|
| 81 | +
|
| 82 | +GET /magazines/:magazine_id/ads index display a list of all ads for a specific magazine |
| 83 | +GET /magazines/:magazine_id/ads/new new return an HTML form for creating a new ad belonging to a specific magazine |
| 84 | +POST /magazines/:magazine_id/ads create create a new ad belonging to a specific magazine |
| 85 | +GET /magazines/:magazine_id/ads/:id show display a specific ad belonging to a specific magazine |
| 86 | +GET /magazines/:magazine_id/ads/:id/edit edit return an HTML form for editing an ad belonging to a specific magazine |
| 87 | +PATCH/PUT /magazines/:magazine_id/ads/:id update update a specific ad belonging to a specific magazine |
| 88 | +DELETE /magazines/:magazine_id/ads/:id destroy delete a specific ad belonging to a specific magazine |
| 89 | +
|
| 90 | +
|
| 91 | +resources :publishers do |
| 92 | + resources :magazines do |
| 93 | + resources :photos |
| 94 | + end |
| 95 | +end |
| 96 | +``` |
| 97 | + |
| 98 | +## Shallow nesting |
| 99 | +``` |
| 100 | +resources :posts do |
| 101 | + resources :comments, only: [:index, :new, :create] |
| 102 | +end |
| 103 | +resources :comments, only: [:show, :edit, :update, :destroy] |
| 104 | +
|
| 105 | +resources :posts do |
| 106 | + resources :comments, shallow: true |
| 107 | +end |
| 108 | +
|
| 109 | +resources :posts, shallow: true do |
| 110 | + resources :comments |
| 111 | + resources :quotes |
| 112 | + resources :drafts |
| 113 | +end |
| 114 | +
|
| 115 | +shallow do |
| 116 | + resources :posts do |
| 117 | + resources :comments |
| 118 | + resources :quotes |
| 119 | + resources :drafts |
| 120 | + end |
| 121 | +end |
| 122 | +
|
| 123 | +scope shallow_path: "sekret" do |
| 124 | + resources :posts do |
| 125 | + resources :comments, shallow: true |
| 126 | + end |
| 127 | +end |
| 128 | +``` |
| 129 | + |
| 130 | +## Routing concerns |
| 131 | +``` |
| 132 | +concern :commentable do |
| 133 | + resources :comments |
| 134 | +end |
| 135 | + |
| 136 | +concern :image_attachable do |
| 137 | + resources :images, only: :index |
| 138 | +end |
| 139 | +
|
| 140 | +
|
| 141 | +
|
| 142 | +resources :messages, concerns: :commentable |
| 143 | + |
| 144 | +resources :posts, concerns: [:commentable, :image_attachable] |
| 145 | +
|
| 146 | +
|
| 147 | +
|
| 148 | +resources :messages do |
| 149 | + resources :comments |
| 150 | +end |
| 151 | + |
| 152 | +resources :posts do |
| 153 | + resources :comments |
| 154 | + resources :images, only: :index |
| 155 | +end |
| 156 | +
|
| 157 | +
|
| 158 | +namespace :posts do |
| 159 | + concerns :commentable |
| 160 | +end |
| 161 | +``` |
| 162 | + |
| 163 | + |
| 164 | +## Links |
| 165 | +``` |
| 166 | +<%= link_to 'Ad details', magazine_ad_path(@magazine, @ad) %> |
| 167 | +<%= link_to 'Ad details', url_for([@magazine, @ad]) %> |
| 168 | +<%= link_to 'Ad details', [@magazine, @ad] %> |
| 169 | +<%= link_to 'Magazine details', @magazine %> |
| 170 | +<%= link_to 'Edit Ad', [:edit, @magazine, @ad] %> |
| 171 | +``` |
| 172 | + |
| 173 | +## Adding More RESTful Actions |
| 174 | +``` |
| 175 | +resources :photos do |
| 176 | + member do |
| 177 | + get 'preview' |
| 178 | + end |
| 179 | +end |
| 180 | +
|
| 181 | +resources :photos do |
| 182 | + get 'preview', on: :member |
| 183 | +end |
| 184 | +
|
| 185 | +resources :photos do |
| 186 | + collection do |
| 187 | + get 'search' |
| 188 | + end |
| 189 | +end |
| 190 | +
|
| 191 | +resources :photos do |
| 192 | + get 'search', on: :collection |
| 193 | +end |
| 194 | +``` |
| 195 | + |
| 196 | + |
| 197 | +``` |
| 198 | +get ':controller(/:action(/:id))' |
| 199 | +
|
| 200 | +get ':controller(/:action(/:id))', controller: /admin\/[^\/]+/ |
| 201 | +
|
| 202 | +get ':controller/:action/:id/with_user/:user_id' |
| 203 | +
|
| 204 | +``` |
| 205 | + |
| 206 | +## HTTP Verb Constraints |
| 207 | +``` |
| 208 | +match 'photos', to: 'photos#show', via: [:get, :post] |
| 209 | +
|
| 210 | +match 'photos', to: 'photos#show', via: :all |
| 211 | +``` |
| 212 | + |
| 213 | +## Segment Constraints |
| 214 | +``` |
| 215 | +get 'photos/:id', to: 'photos#show', constraints: { id: /[A-Z]\d{5}/ } |
| 216 | +
|
| 217 | +get '/stories', to: redirect('/posts') |
| 218 | +get '/stories/:name', to: redirect('/posts/%{name}') |
| 219 | +get '/stories/:name', to: redirect {|params, req| "/posts/#{params[:name].pluralize}" } |
| 220 | +
|
| 221 | +
|
| 222 | +root to: 'pages#main' |
| 223 | +root 'pages#main' # shortcut for the above |
| 224 | +``` |
| 225 | + |
0 commit comments