AndrewP
AndrewP

Reputation: 1087

Rails routes helper not updating to new "match"ed route

I have a "list" model and "ListsController" controller for it. By default, the route for lists was /lists/1, /lists/1/edit/, etc. I changed my routes.rb file to make it so the show path was "/:id", the new path was "/new".

Here's my routes file:

ToDo::Application.routes.draw do
  root to: 'pages#home'

  match '/about', to: 'pages#about'
  match '/contact', to: 'pages#contact'
  match '/help', to: 'pages#help'

  resources :lists

  match '/new', to: 'lists#new'
  match '/:id', to: 'lists#show'
  match '/:id/new', to: 'lists#new_item'
end

I can access a list by doing "localhost:3000/1" perfectly fine. But now I'm trying to use link_to, and when I do "link_to "List", list", it generates a url to the original route, which is "localhost:3000/lists/1".

Does anyone know how to fix this? Is there anything I should be doing better with my routes?

Thanks!

Upvotes: 0

Views: 78

Answers (2)

Arie Xiao
Arie Xiao

Reputation: 14082

You will need to specify as: 'name' option to create a named route for your match rules, and to overwrite the named route provided by resource :lists.

resource :lists

match '/new', to: 'lists#new', as: 'new_list'
match '/:id', to: 'lists#show', as: 'list'

Upvotes: 0

samuil
samuil

Reputation: 5081

Instead of using match you could simply provide alternative path for resources:

resources :lists, path: ''

Upvotes: 1

Related Questions