2012-02-27 78 views
1

如何使用限制:子域

constraints :subdomain => 'api',:format => :json do 
end 

和所有其他人一樣WWW,零和泛型重定向到www或無(域)。

沒有泛型子域,只是api,只有格式json。

constraints :subdomain => 'api',:format => :json do 
    root :to => "posts#index", :defaults => { :format => :json } 
    get "posts" => "posts#index", :defaults => { :format => :json } 
end 

如何獲得默認JSON,如果我從api.domain.com/posts而不是/posts.json 這不是必須的功能訪問,但很酷。 現在我需要重定向到域如果請求不是json或沒有找到或呈現爲json。

api.domain.com/posts 

應: 呈現爲JSON,重定向到domain.com/post或顯示未找到頁面。

回答

5

根據Rails Guide on Routing它應該是這樣的。

constraints :subdomain => "api" do 
    resources :your_resources_go_here, :defaults => { :format => :json } 
end 
+0

其實不行,讓我編輯一下問題。 – flakerimi 2012-02-27 22:27:42

+0

就是這樣。不知何故是有道理的。 – flakerimi 2012-02-27 23:12:40