1

我的Rails應用程序被設置爲在此RailsCast描述使用子域名:子域在Rails的嵌套資源

http://railscasts.com/episodes/221-subdomains-in-rails-3

然而,現在,路徑呈現這樣的:

http://organization.domain.com/organizations/1/edit

我有控制器設置選擇基於子域的組織,所以我想知道是否有一種方法去除路徑的/ organizations /:id部分,例如:

link_to edit_organization(@organization) 

http://organization.domain/edit,而不是http://organization.domain/organizations/:id/edit

由於有將內組織(人,捐贈等),有很多嵌套的資源,它是URL的最終不會令人難以置信的長,這是非常重要路徑生成方法仍然非常簡單。

有沒有辦法做到這一點?

+0

正如你所說,如果你有很多嵌套的資源。你可能想看看這個http://weblog.jamisbuck.org/2007/2/5/nesting-resources。 這無疑是一個更好的方法去做。 – Gooner 2012-07-16 15:21:20

回答

0

你可以使用像一個路線:

resource :organization, :path => "" 

的將您的網址砍倒的「http://organization.domain/:ID/edit`。

擺脫:id是棘手的,我不認爲它可以直接完成。我會做的是這樣的:

resource :organization, :path => "", :only => [] do 
    match "index", :via => :get 
    match "new", :via => :get 
    match "show", :via => :get, :constraints => {:subdomain => /[a-zA-Z]+/} 
    match "edit", :via => :get, :constraints => {:subdomain => /[a-zA-Z]+/} 
    match "update", :via => :put, :constraints => {:subdomain => /[a-zA-Z]+/} 
    match "create", :via => :post 
end 

不是很乾,但我認爲它應該工作。