[英]How to set default format for routing in Rails?
路由的代碼如下:
resources :orders, only: [:create], defaults: { format: 'json' }
resources :users, only: [:create, :update], defaults: { format: 'json' }
resources :delivery_types, only: [:index], defaults: { format: 'json' }
resources :time_corrections, only: [:index], defaults: { format: 'json' }
可以使用1個字符串為所有資源設置默認格式,每行不帶“defaults”哈希值? 謝謝。
嘗試這樣的事情:
scope format: true, defaults: { format: 'json' } do
resources :orders, only: [:create]
resources :users, only: [:create, :update]
resources :delivery_types, only: [:index]
resources :time_corrections, only: [:index]
end
我寧願為application_controller添加方法。 並在我想要的過濾器之前使用它。
class ApplicationController < ActionController::Base
...
private
...
def set_default_format
params[:format] ||= "json"
end
end
class UsersController < ApplicationController
before_filter :set_default_format, only: [:create]
...
end
在這種情況下,默認格式對新開發人員來說並不意外,因為通常routes.rb
很大且很麻煩
這對我有用:
scope defaults: { format: 'json' } do
resources :users, only: [:index]
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.