2011-01-07 136 views
0

我環顧四周,有一段時間了,但我不知道如何最好地形容我的要求給Google,所以認爲我會問這裏;)自定義路由Rails中

在軌,我知道當你窩寧靜的路線,你通常得到的東西,如:

http://localhost/categories/1/articles/2

如果你想要的東西更有意義,你可以使用蛞蝓或friendly_id要達到這樣的

http://localhost/categories/all-your-needs/articles/rock-out-with-this-article

(假設你有獨特的名稱)。

我的問題是,我該如何從URL重寫卸下控制器,所以你得到的東西,如:

http://localhost/all-your-needs/rock-out-with-this-article

這可能嗎?

回答

0

是的。您可以使用這樣的事情:

梁2:

map.show_article ':category/:article', :controller => "articles", :action => "show" 

編輯:

確定。在這裏,您有其他REST操作的URL:

map.edit_article ':category/:article/edit', :controller => "articles", :action => "edit". 

對於更新添加:conditions => { :method => :post }前一個。

如需刪除,請使用第一個:conditions => { :method => :delete }

對於新的創造,你可以使用:

map.new_article ':category/new', :controller => "articles", :action => "new" 

和創建相同但:conditions => { :method => :post }。我希望我能夠幫助你!

+0

實際上,Rolpege提供的功能確實有效......但僅適用於一個操作(顯示)。我如何擴展它以包含所有操作(或嵌套更多控制器)? – 2011-01-07 19:39:59