【问题标题】:Subdomain for nested resources in RailsRails 中嵌套资源的子域
【发布时间】:2012-07-16 00:11:48
【问题描述】:

我的 rails 应用程序设置为使用此 RailsCast 中所述的子域:

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 最终不要过长并且路径生成方法保持非常简单很重要。

有没有办法做到这一点?

【问题讨论】:

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 routes subdomain


【解决方案1】:

您可以使用如下路线:

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

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

【讨论】:

    猜你喜欢
    • 2015-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多