2010-12-14 101 views
2

是否有可能具有變量名稱空間?我有寧靜的資源,像下面這樣:Rails 3具有變量名稱空間的路由資源

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 

UPDATE:當使用上述範圍的指令,我得到像我的路線要:

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

我現在可以在引用:editionApplicationController

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

但是params [:edition]解決了什麼問題?這是嚴重的foobar或bazbam?如果是的話就贏了!我學到了東西! – Cory 2010-12-14 15:14:14

+1

'範圍':'版'真的有用!但是在url助手中引發了很多麻煩,例如:'link_to(「View Article」,@article)','form_for(@article)'和'redirect_to(@article)'會引發錯誤。我認爲'scope'的設計目的是針對靜態命名空間的。你爲什麼不把這個版本作爲參數追加到url的末尾呢?如果遵循導軌方式,這將節省您的時間。 – Kevin 2010-12-14 15:31:34

+1

@Kevin - 我真的想保持URL RESTful,就我而言,每個資源對於:edition來說確實是唯一的,所以這並不矛盾REST的意圖。作爲參數傳遞可能會起作用,但我希望儘可能保持URL的人性化。另一種選擇是允許用戶通過application.html.erb中的下拉菜單切換版本並將該值存儲在cookie中。 – 2010-12-14 15:45:10

回答

0

其實,你可以做到以下幾點:

my_var = "persons" 
resources my_var.to_sym 

一個字符串的方法to_sym它變成一個符號

0

如果你不知道的版本可能的值 - 那麼你就可以不使用名稱空間,看起來他們可能已經解決了這個問題。

這就是說,我只是手工製作它們 - 你的情況在這裏看起來像是理想情況下的前述資源,並直接進入手工製作的路徑。

相關問題