是否可以拥有变量名称空间?我有以下资源:
resources :articles
resources :persons
但我需要将这些范围放在变量名称空间中,以便它响应表单的URL:
':edition/:controller/:action/:id'
例如:
/foobar/article/edit/123
或/bazbam/person/edit/345
为每个资源。这可能是resources
方法,还是我必须手工制作这些?我不会提前知道:edition
的可能值;这些内容会在before_filter
中的ApplicationController
中查找。
这就是我需要做的全部吗?
scope ':edition' do
resources :articles
resources :matches
resources :teams
end
更新:当使用上面的范围指令时,我得到了我想要的路线:
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
:
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
现在我只想确保这是实现这一目标的最佳方式。
答案 0 :(得分:0)
实际上,您可以执行以下操作:
my_var = "persons"
resources my_var.to_sym
字符串上的to_sym
方法将其更改为符号
答案 1 :(得分:0)
如果你不知道版本的可能值 - 那么你就不能使用似乎可能已经解决了这个问题的命名空间。
那就是说,我只是手工制作它们 - 你的情况似乎是理想的情况,不仅仅是资源,而是直接进入手工制作的道路。