rails pretty routing
-
29-06-2021 - |
题
I have news conroller where news select by year.
in routes.rb I have
get 'news' => 'pages#news'
get 'news/:year' =>'pages#news'
url http://localhost:3000/news/2012
is ok but when I try to create link, it's no pretty.
for example: = link_to year, news_path(year: 2012)
I see http://localhost:3000/news?year=2012
instead http://localhost:3000/news/2012
How I can fix it?
p.s. item it's model for news controller
解决方案
Replace with one line:
get 'news/(:year)' =>'pages#news'
Explanations:
Router rule is: first match first served. get 'news'
matches your path so it adds year
as a query param.
Alternate solution would be to switch your lines but it's better to keep only one.
不隶属于 StackOverflow