2

我目前正在设置一些这样的路线:

  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'}为这个集群中的所有路由设置,而不必为每个路由都写出来。

4

1 回答 1

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

于 2014-08-06T22:07:31.720 回答