开发者

RoR Routes problems . Cities in the start of urls

I have many links like these: /articles/...开发者_如何学JAVA /albums/... /photos/... etc.

I need to use the name of the city in the beginning of urls if the user has choosed it: /city_name/articles/... /city_name/albums/... /city_name/photos/...

But previous links shoul work too (/articles/..., /albums/...)

I tried this:

map.articles '/articles/', :controller => "zags", :action => "cities", :using_city => 1
map.articles '/:city_url/articles/', :controller => "zags", :action => "cities", :using_city => 1

<%= link_to "London articles", articles_path, { :city_url => "london", :using_city => 1 } %>

But it does not work. What should I change? I know, that I can change one of map names "map.articles", but I want to use the same route names in my View code.How can I do it?


You should be able to do this with a single route:

map.articles '(/:city)/articles', :controller => 'zags', :action => 'cities'

<%= link_to "Articles", articles_path %>
<%= link_to "London Articles", articles_path(:city => "london") %>


I guess you just creating your url wrong. It should be like:

<%= link_to "London articles", articles_path(:city_url => "london", :using_city => 1) %>

You may have conflicts with your articles path as well, because you are using map.articles twice. Maybe the second could be map.articles_by_city ... to have proper naming. And in this case your link changes like:

<%= link_to "London articles", articles_by_city_path(:city_url => "london", :using_city => 1) %>

Btw if you are using rails 3 I suggest the following route definition.

match "(:city_url/)articles/", :controller => "zags", :action => "cities", :using_city => 1, :as => "articles"

This sets up for you both cases, so the :city_url became an optional parameter. In this case your path will be just articles_path and the url will look like /articles/.

If you pass articles_path("london") your url will look like /london/articles/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜