建立: 2017/06/29json
表示全部有效路径 | rails routes 路径类内部能够带参数, 表示请求的参数app sample_test1_method(data_id: d.id) # 控制器名_方法名_path
|
新建带命名空间的controller | rails generate controller test::con t1 t2 t3 t4 |
view的位置 | views/模块名/类名/... |
route的记述原则 | 通用的记在后面 root记在最后 |
获取 | GET |
创造 | POST |
更新 | PATCH/PUT 区别: patch为部分更新,更新传上去的 put更新所有,必须上传所有 |
删除 | DELETE |
RESTful 接口定义 | |||||||||||||||||||||||||||||||||||||||||||||||||
位置 | /config/route.rb | ||||||||||||||||||||||||||||||||||||||||||||||||
resources方法 | resources :names [,...] 注: 控制器名为复数url
|
||||||||||||||||||||||||||||||||||||||||||||||||
由resources自动定义的路径 | resources: :samples对应的控制器名 SamplesController.rb 注意:路径中的参数也是能够拿出来的,params[:format]
|
||||||||||||||||||||||||||||||||||||||||||||||||
由resources自动定义的路径助手 | 命令行: rails routes 网页版: http://localhost:3000/rails/info/routes _path和_url的区别,url直接生成绝对路径(http://...) id也能够指定模型 指定格式, url(..., format: :json)
|
||||||||||||||||||||||||||||||||||||||||||||||||
单一的resource定义 | 用于惟一的资源(如设定等) 注: 控制器名为复数命令行
命令行: rails routes 网页版: http://localhost:3000/rails/info/routes _path和_url的区别,url直接生成绝对路径(http://...) id也能够指定模型 指定格式, url(..., format: :json)
|
||||||||||||||||||||||||||||||||||||||||||||||||
RESTful 接口自定义 option |
|||||||||||||||||||||||||||||||||||||||||||||||||
resources/resource的选项 | |||||||||||||||||||||||||||||||||||||||||||||||||
constraints | 对路径参数设置限制 注: Controller为TestControllerorm |
||||||||||||||||||||||||||||||||||||||||||||||||
限制类 制約クラス |
用正规表现没法实现的复杂限制用限制类
class TimeConstraint def match?(request) current = Time.now current.hour >= 9 && current.hour <= 18 end end ------------------------------------------------------ resources :test, constraints: TimeConstraints.new |
||||||||||||||||||||||||||||||||||||||||||||||||
去除format | 选项 format: false 默认为true |
||||||||||||||||||||||||||||||||||||||||||||||||
改变用的视图控制器名 |
|
||||||||||||||||||||||||||||||||||||||||||||||||
命名空间 | namespace
|
||||||||||||||||||||||||||||||||||||||||||||||||
限定使用的方法(action) | resources :tests, only: [:show, :index] resource :test, except[:index] 注: 用数组包含方法的符号(Symbol)
|
||||||||||||||||||||||||||||||||||||||||||||||||
增长方法(action) | collection 对应多个对象(object) member 对应一个对象(object) 能够省略任意一个或所有(也就是不加东西) resources :name do [collection do method action ... end] [member do method action end] end 或者 resources :name, on: :member/:controller
collection do get :test1 get :test2 end member do get :test3 end end |
||||||||||||||||||||||||||||||||||||||||||||||||
改变方法(action)指向的url名字 | path_name: {原名: :新名, ...} 例: resources :test, path_name: {new: :insert, edit: :revise} |
||||||||||||||||||||||||||||||||||||||||||||||||
嵌套 | resources :test1 do resources :test2 end
|
||||||||||||||||||||||||||||||||||||||||||||||||
去除嵌套路径中被嵌套部分的母路径 原(去除复制的复制) |
shallow:true
resources :test2, shallow:true end |
||||||||||||||||||||||||||||||||||||||||||||||||
重复的共有化 | 提取重复部分 对象 concern :name do
...
end
使用 # 单个 resources :messages, concerns: :commentable # 多个时用数组, 按数组顺序展开 resources :articles, concerns: [:commentable, :image_attachable] # 能够展开在任何位置 concerns :sample
● 参数
例 # 只提取具体方法 concern :sample do get: test1, on:member end # 提取整个resource/resources concern :commentable do resources :comments end concern :image_attachable do resources :images, only: :index end
|
||||||||||||||||||||||||||||||||||||||||||||||||
设定默认值 | get 'sample/:model/:id', default: {model: } | ||||||||||||||||||||||||||||||||||||||||||||||||
非RESTful路径的定义 | |||||||||||||||||||||||||||||||||||||||||||||||||
基本 |
match pattern => action, via: verb [opts] 例: match '/user/:id/show' => 'sample#t1', via: [:get] # 只定义一个方法能够直接指定 match '/user/:id/show' => 'sample#t1', via: :get # 也能够直接用 get '/user/:id/show' => 'sample#t1' # 路径和方法名一直时能够省略方法名指定 get 'sample/t1' # get 'sample/t1' => 'sample#t1'
|
||||||||||||||||||||||||||||||||||||||||||||||||
设置首页 | root to: url 例子 root to: 'devise/user#t1' |