2010-09-17 53 views
0

我想改變的回報率,以我想要的默認路由: 考慮下面的例子...問題航線

:controller/:action/:id 

,這將給你的路線在瀏覽器中: http://localhost:3000/controller/action/id

現在我想將它更改爲...

http://localhost:3000/this-is-what-i-want/id

我們可以得到控制和操作的別名,以及喜歡...

resources :controller, :as => "my-custom-name" 

,如果你想爲行動別名,然後

resources :controller, :path_names => { :action => 'my-custome-name-1', :action => 'my-custome-name-2' } 

BUT我想改變控制器和動作立即...如果你注意到上述http://localhost:3000/this-is-what-i-want/id路徑的問題...

需要幫助...提前 感謝...

回答

1

您需要命名路線。

在Rails2:

map.a_name 'this-is-what-i-want/:id', :controller => 'controller_name', :action => 'action_name' 

在Rails3中:

match 'this-is-what-i-want/:id' => 'controller_name#action_name' 
+0

現在如果我想:map.a_name '這 - ,是什麼,我想做/動態名稱/:身份證' ,:controller =>'controller_name',:action =>'action_name'...我如何在rails中爲「動態名稱」設置鏈接......? – 2010-09-17 14:12:10

+0

然後按照Jesse Wolgamott的建議使用休息路線 – Yannis 2010-09-17 20:59:09

0

你想使用休息的路線,而不是控制器/動作

我將使用「球」,而不是「這-is-什麼,我想做」

resources :balls 

然後,當你鏈接到一個球,做link_to(ball.name, ball)。

這會給你的http://localhost:3000/balls/45

rails rest cheatsheet的鏈接是一個好的開始。