【问题标题】:Rails 3 Routing Resources with Variable Namespace带有变量命名空间的 Rails 3 路由资源
【发布时间】: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


【解决方案1】:

其实你可以这样做:

my_var = "persons"
resources my_var.to_sym

字符串上的to_sym 方法将其更改为符号

【讨论】:

    【解决方案2】:

    如果您不知道 edition 的可能值 - 那么您就不能使用似乎可以解决此问题的命名空间。

    也就是说,我只是手工制作它们 - 你的案例在这里似乎是理想的案例,而不是资源并直接走向手工制作的路径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-10
      • 2015-07-02
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2012-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多