具有可变命名空间的Rails 3路由资源

问题描述:

是否可以使用变量名称空间?我有以下类似的资源:

Is it possible to have a variable namespace? I have restful resources like the following:

resources :articles
resources :persons

但是我需要将它们限定在变量名称空间中,这样它才能响应以下形式的URL:

But I need to scope these inside a variable namespace, such that it responds to URLs of the form:

':edition/:controller/:action/:id' 

例如:

/ foobar / article / edit / 123 / bazbam / person / edit / 345

每个资源。 资源方法是否可行,还是我必须手工制作?我不会提前知道:edition 的可能值;这些在我的 ApplicationController 中的 before_filter 中进行查找。

for each of the resources. Is this possible with the resources method, or must I hand-craft these? I will not know the possible values for :edition ahead of time; these get looked up in a before_filter in my ApplicationController.

这就是我需要做的吗?

scope ':edition' do
  resources :articles
  resources :matches
  resources :teams
end

更新:使用上面的scope指令时,我会得到想要的路由:

    articles GET    /:edition/articles(.:format)          {:action=>"index", :controller=>"articles"}
             POST   /:edition/articles(.:format)          {:action=>"create", :controller=>"articles"}
 new_article GET    /:edition/articles/new(.:format)      {:action=>"new", :controller=>"articles"}
edit_article GET    /:edition/articles/:id/edit(.:format) {:action=>"edit", :controller=>"articles"}
     article GET    /:edition/articles/:id(.:format)      {:action=>"show", :controller=>"articles"}
             PUT    /:edition/articles/:id(.:format)      {:action=>"update", :controller=>"articles"}
             DELETE /:edition/articles/:id(.:format)      {:action=>"destroy", :controller=>"articles"}
     matches GET    /:edition/matches(.:format)           {:action=>"index", :controller=>"matches"}
             POST   /:edition/matches(.:format)           {:action=>"create", :controller=>"matches"}
   new_match GET    /:edition/matches/new(.:format)       {:action=>"new", :controller=>"matches"}
  edit_match GET    /:edition/matches/:id/edit(.:format)  {:action=>"edit", :controller=>"matches"}
       match GET    /:edition/matches/:id(.:format)       {:action=>"show", :controller=>"matches"}
             PUT    /:edition/matches/:id(.:format)       {:action=>"update", :controller=>"matches"}
             DELETE /:edition/matches/:id(.:format)       {:action=>"destroy", :controller=>"matches"}
       teams GET    /:edition/teams(.:format)             {:action=>"index", :controller=>"teams"}
             POST   /:edition/teams(.:format)             {:action=>"create", :controller=>"teams"}
    new_team GET    /:edition/teams/new(.:format)         {:action=>"new", :controller=>"teams"}
   edit_team GET    /:edition/teams/:id/edit(.:format)    {:action=>"edit", :controller=>"teams"}
        team GET    /:edition/teams/:id(.:format)         {:action=>"show", :controller=>"teams"}
             PUT    /:edition/teams/:id(.:format)         {:action=>"update", :controller=>"teams"}
             DELETE /:edition/teams/:id(.:format)         {:action=>"destroy", :controller=>"teams"}

我现在可以引用:edition 在我的 ApplicationController 中:

I'm now able to reference :edition in my ApplicationController:

class ApplicationController < ActionController::Base
  protect_from_forgery

  before_filter :authenticate_user!
  before_filter :get_edition

  def get_edition
    @edition = Edition.first(:conditions => { :FriendlyName => params[:edition] } )
  end

end

现在我只想确保这是最好的

Now I just want to make sure this is the best way to accomplish this.

如果您不知道版本的可能值,则不能使用命名空间看来他们可能已经解决了这个问题。

If you don't know the possible values for edition - then you can't use namespaces which seem like they might have solved this issue.

那是说,我只是手工制作-您的案例在这里是理想的案例,它超越了资源,直接去手工制作路径。

That said, I'd just handcraft them - your case here seems like the ideal case foregoing resources and going straight to a handcrafted path.