resources :photos
会创建七个不同的路由,全部映射到 Photos 控制器上:
HTTP 方法 路径 控制器#动作 作用
GET /photos photos#index 显示所有图片
GET /photos/new photos#new 显示新建图片的表单
POST /photos photos#create 新建图片
GET /photos/:id photos#show 显示指定的图片
GET /photos/:id/edit photos#edit 显示编辑图片的表单
PATCH/PUT /photos/:id photos#update 更新指定的图片
DELETE /photos/:id photos#destroy 删除指定的图片
get '/patients/:id', to: 'patients#show', as: 'patient'
@patient = Patient.find(17)
<%= link_to 'Patient Record', patient_path(@patient) %>
单数资源
get 'profile', to: 'users#show'
get 'profile', to: :show
resource :geocoder
HTTP 方法 路径 控制器#动作 作用
GET /geocoder/new geocoders#new 显示新建 geocoder 的表单
POST /geocoder geocoders#create 新建 geocoder
GET /geocoder geocoders#show 显示唯一的 geocoder 资源
GET /geocoder/edit geocoders#edit 显示编辑 geocoder 的表单
PATCH/PUT /geocoder geocoders#update 更新唯一的 geocoder 资源
DELETE /geocoder geocoders#destroy 删除 geocoder 资源
有时需要使用同个控制器处理单数路由(例如 /account)和复数路由(例如 /accounts/45),把单数资源映射到复数控制器上。例如,resource :photo 和 resources :photos 分别声明单数和复数路由,映射到同个控制器(PhotosController)上。
有个一直存在的问题导致 form_for 无法自动处理单数资源。为了解决这个问题,可以直接指定表单的 URL,例如:
form_for @geocoder, url: geocoder_path do |f|
会创建七个不同的路由,全部映射到 Photos 控制器上:
HTTP 方法 路径 控制器#动作 作用
GET /photos photos#index 显示所有图片
GET /photos/new photos#new 显示新建图片的表单
POST /photos photos#create 新建图片
GET /photos/:id photos#show 显示指定的图片
GET /photos/:id/edit photos#edit 显示编辑图片的表单
PATCH/PUT /photos/:id photos#update 更新指定的图片
DELETE /photos/:id photos#destroy 删除指定的图片
get '/patients/:id', to: 'patients#show', as: 'patient'
@patient = Patient.find(17)
<%= link_to 'Patient Record', patient_path(@patient) %>
单数资源
get 'profile', to: 'users#show'
get 'profile', to: :show
resource :geocoder
HTTP 方法 路径 控制器#动作 作用
GET /geocoder/new geocoders#new 显示新建 geocoder 的表单
POST /geocoder geocoders#create 新建 geocoder
GET /geocoder geocoders#show 显示唯一的 geocoder 资源
GET /geocoder/edit geocoders#edit 显示编辑 geocoder 的表单
PATCH/PUT /geocoder geocoders#update 更新唯一的 geocoder 资源
DELETE /geocoder geocoders#destroy 删除 geocoder 资源
有时需要使用同个控制器处理单数路由(例如 /account)和复数路由(例如 /accounts/45),把单数资源映射到复数控制器上。例如,resource :photo 和 resources :photos 分别声明单数和复数路由,映射到同个控制器(PhotosController)上。
有个一直存在的问题导致 form_for 无法自动处理单数资源。为了解决这个问题,可以直接指定表单的 URL,例如:
form_for @geocoder, url: geocoder_path do |f|