2014-01-09 3 views
5

Es ist der folgende Code für das Routing: ohne ‚Vorgaben‘ Hash auf jeder ZeileWie wird das Standardformat für das Routing in Rails festgelegt?

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' } 

Es ist möglich, Standardformat für alle Ressourcen unter Verwendung von 1-String zu setzen? Vielen Dank.

+0

Ist nicht das Routing von verschiedenen Anforderungstypen aus der Header-Anfrage abgeleitet (http: // stackoverflow.com/questions/1595424/request-format-returning/1595453#1595453), nicht in einem param? –

Antwort

6

versuchen, etwas wie folgt aus:

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 
2

Ich würde eher fügen Methode application_controller. Und benutze es wie vorher filter wo ich will.

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 

In diesem Fall Standardformat für neue Entwickler würde, da in der Regel keine Überraschung, routes.rb groß und unhandlich ist