Frage

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

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top