【问题标题】:Rails routes helper not updating to new "match"ed routeRails 路由助手未更新到新的“匹配”路由
【发布时间】:2013-06-19 09:41:34
【问题描述】:

我有一个“列表”模型和“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”。

有谁知道如何解决这个问题?我的路线有什么需要改进的地方吗?

谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    您可以简单地为资源提供替代路径,而不是使用match

    resources :lists, path: ''
    

    【讨论】:

      【解决方案2】:

      您需要指定as: 'name' 选项来为您的匹配规则创建命名路由,并覆盖resource :lists 提供的命名路由。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多