设置:路由群集的默认值

时间:2013-04-11 18:09:00

标签: ruby-on-rails

我目前正在设置许多路线:

  post '/create', :to => 'reports#create', :defaults => {:format => 'json'}
  get '/all', :to => 'reports#index', :defaults => {:format => 'json'}
  delete '/:id', :to => 'reports#destroy', :defaults => {:format => 'json'}
  get '/:id', :to => 'reports#show', :defaults => {:format => 'json'}
  root :to => 'reports#index', :defaults => {:format => 'json'}

我想知道是否有任何方法可以为此群集中的所有路由设置:defaults => {:format => 'json'},而无需为每个路由写出来。

1 个答案:

答案 0 :(得分:2)

尝试这样的事情:

defaults { :format => :json } do
  post '/create', :to => 'reports#create'
  get '/all', :to => 'reports#index'
  delete '/:id', :to => 'reports#destroy'
  get '/:id', :to => 'reports#show'
  root :to => 'reports#index'
end

资源: http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Scoping.html#method-i-defaults