routes 详解

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|
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值