Rails 3 Routing Resources with Variable Namespace

2019-05-23 14:03发布

问题:

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

resources :articles
resources :persons

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

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

for example:

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

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.

Is this all I need to do?

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

UPDATE: When using the scope directive above, I get routes like I want:

    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"}

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.

回答1:

Actually, you can just do the following :

my_var = "persons"
resources my_var.to_sym

The to_sym method on a string changes it to a symbol



回答2:

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.