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 

不是很干,但我认为它应该工作。