- Rails收到请求:GET /patients/17
查询路由,找到controller的action: get ‘/patient/:id’,to: ‘patients#show’ - 生成路径和URL地址
route.rb :get ‘/patient/:id’,to: ‘patients#show’,as: ‘patient1’
model: Patient
Controller(patient_controller.rb): @patient1=Patient.find(params[:id])
View(patient/show.html.erb): <%= link_to ‘Patient Record’,Patient_path(@patient1)%>
(patient_path: Patient-model) - 资源路由resource:Rails的默认风格,声明所有常用动作
1
2
3
4
5
6e.g :photos(复数资源)
生成路径和URL的辅助方法:
(1)photos_path 返回/photos
(2)new_photo_path 返回/photos/new
(3)edit_photo_path(:id) 返回/photos/:id/edit
(4)photo_path(:id) 返回/photos/:id
1 | e.g :geocoder(单数资源)映射到Geocoders控制器动作上 |
- 指定action和controller
get ‘profile’,to: ‘users#show’
get ‘profile’,to: :show, controller: ‘users’ - 指定命名空间controller/admin/articles_controller.rb指定命名空间为Admin
Controller: class Admin::ArticlesController
route.rb :(1) /admin/articles/new1
2
3namespace :admin do
resourcs :articles
end
(2)/articles映射到Admin::Articles1
2
3scope module: 'admin' do
resources :articles
end
(3)/admin/articles映射到Articles1
2
3scope '/admin' do
resources :articles
end
嵌套资源