2011-03-08 106 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