2011-03-08 150 views
1

我的問題是我想刪除這個URL的querystring部分,並使其乾淨。Rails路線與乾淨的網址

http://staging.mysite.com/mycontroller?name=/philippines/about-manila 

目前我有MyController.index(),我解析了這個方法中的name參數。

我最終希望是這樣的:

http://staging.mysite.com/mycontroller/philippines/about-manila 

參數部分'philippines/about-manila'可以有參數任意數量,像

http://staging.mysite.com/mycontroller/philippines/about-manila/people 

我怎麼能在路線做到這一點?

+0

你使用rails2.x或rails3.x? – 2011-03-08 14:05:07

+0

即時通訊使用rails 2.x – r2b2 2011-03-08 14:06:09

+0

http://stackoverflow.com/questions/4690298/how-do-we-identify-parameters-in-a-dynamic-url/4705154#4705154 – jdl 2011-03-08 14:12:12

回答

2

它真的是任意的 - 或只是變量?

如果只是變量,可以在括號包含可選的參數:

match '/:city(/:section(/:subsection))', :controller => :mycontroller, 
    :action => :index 

對於Rails的2.X:

map.connect '/:city(/:section(/:subsection))', :controller => :mycontroller, 
    :action => :index 
+0

這是隻限於3個網址段? – r2b2 2011-03-08 14:15:54

+0

這是3段。 – 2011-03-08 17:07:08

1

對於Rails的2.X可以使用

map.my_route '/:my_param', :controller => :mycontroller, :action => :index 

然後在您的控制器中,您可以訪問

params[:my_param] 

如果你想鏈接只是

my_route_path(:my_param => "mytekst") 
+0

這看起來很有趣..我會試試這個..謝謝! – r2b2 2011-03-08 14:15:26