Reputation: 2670
How to use
constraints :subdomain => 'api',:format => :json do
end
and all others like www,nil and generics to redirect to www or nil (domain).
No generic subdomain, just api and only format json.
constraints :subdomain => 'api',:format => :json do
root :to => "posts#index", :defaults => { :format => :json }
get "posts" => "posts#index", :defaults => { :format => :json }
end
How to get default json if I access from api.domain.com/posts instead of /posts.json This is not must have feature, but is cool. Now I need an redirect to domain if request is not json or not Found or render as json.
api.domain.com/posts
Should: render as json, redirect to domain.com/post or show not found page.
Upvotes: 1
Views: 6115
Reputation: 3011
According to the Rails Guide on Routing it should go something like this.
constraints :subdomain => "api" do
resources :your_resources_go_here, :defaults => { :format => :json }
end
Upvotes: 5