【发布时间】:2011-05-25 07:42:01
【问题描述】:
是否可以有一个变量命名空间?我有以下资源:
resources :articles
resources :persons
但我需要在变量命名空间内限定这些范围,以便它响应表单的 URL:
':edition/:controller/:action/:id'
例如:
/foobar/article/edit/123 或 /bazbam/person/edit/345
对于每个资源。 resources 方法可以做到这一点,还是我必须手工制作这些?我不会提前知道:edition 的可能值;这些在我的ApplicationController 中的before_filter 中查找。
这就是我需要做的一切吗?
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"}
我现在可以在我的ApplicationController 中引用:edition:
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
现在我只想确定这是实现这一目标的最佳方式。
【问题讨论】:
-
但是 params[:edition] 解析成什么?是认真的foobar还是bazbam?如果是这样,那就赢了!我学到了一些东西!
-
scope ':edition'真的很管用!但是 url helpers 会引发很多麻烦,例如:link_to("View Article", @article)、form_for(@article)和redirect_to(@article)会引发错误。我认为scope的设计目的是用于静态命名空间。为什么不将版本作为参数附加到 url 的末尾?如果遵循rails方式,它将节省您的时间。 -
@Kevin - 我真的想保持 RESTful 的 URL,在我的例子中,每个资源对于 :edition 来说确实是唯一的,所以这与 REST 的意图并不矛盾。作为参数传递可以工作,但我想保持 URL 尽可能人性化。另一种选择是允许用户通过 application.html.erb 中的下拉菜单切换版本并将该值存储在 cookie 中。
-
是的@Mark,你是对的。你可以根据自己的需求选择一个舒服的方法,反正
scope这个方法真的很管用。
标签: ruby-on-rails rest resources routing ruby-on-rails-3