我有一个“list”模型和“ListsController”控制器。默认情况下,列表的路径是/ lists / 1,/ lists / 1 / edit /等。我更改了我的routes.rb文件以使其显示路径为“/:id”,新路径为“/新”。
这是我的路线档案:
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
我可以通过“localhost:3000/1”完全正常来访问列表。但是现在我正在尝试使用link_to,当我执行“link_to”List“,list”时,它会生成原始路由的URL,即“localhost:3000 / lists / 1”。
有谁知道如何解决这个问题?我的路线有什么用的吗?
谢谢!
答案 0 :(得分:1)
您可以简单地为资源提供备用路径,而不是使用match
:
resources :lists, path: ''
答案 1 :(得分:0)
您需要指定as: 'name'
选项,为匹配规则创建命名路由,并覆盖resource :lists
提供的命名路由。
resource :lists
match '/new', to: 'lists#new', as: 'new_list'
match '/:id', to: 'lists#show', as: 'list'