建立: 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' |