Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-3.1

constraints :subdomain


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.


Solution

  • 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