修复rails 5弃用警告以匹配路由

时间:2016-12-12 09:10:59

标签: ruby-on-rails-5

在我的routes.rb文件中,我有以下

  resources :landings, only: [:index],
    path: '/golf' do
      collection do
        get 'break_70', path: 'how-to-break-70'
      end
  end

生成网址

/golf/how-to-break-70

升级到Rails 5后,会生成以下弃用消息:

DEPRECATION WARNING: Specifying strings for both :path and the route path is deprecated. Change things like this:
  match "break_70", :path => "how-to-break-70"
to this:
  match "how-to-break-70", :as => "break_70", :action => "break_70"

如果我尝试按照这些说明进行操作

 resources :landings, only: [:index],
    match: '/golf' do
      collection do
        match: 'how-to-break-70', as: 'break_70', action: 'break_70'
      end
  end

然后我收到以下错误

syntax error, unexpected ':', expecting keyword_end
        match: 'how-to-break-70', as: 'break_70', action: 'break_70'

如何修改此路线以避免弃用警告?

1 个答案:

答案 0 :(得分:1)

更新答案
我更新了我的答案以修复一个小错误,所以任何读者都可以看到一个有效的代码。感谢@Obromios的小修复。

resources中,它仍应为path:。 在路线定义中,match之后还有一个额外的“:”。如果您via: [:get, :post],则必须指定match 所以最终版本看起来像:

resources :landings, only: [:index], path: '/golf' do
  collection do
    match 'how-to-break-70', as: 'break_70', action: 'break_70', via: [:get, :post]
  end
end

原始答案

resources中,它仍应为path:。 在路线定义中,match之后还有一个额外的“:” 所以最终版本看起来像:

resources :landings, only: [:index], path: '/golf' do
  collection do
    match 'how-to-break-70', as: 'break_70', action: 'break_70'
  end
end

未经测试,但应该可以使用。

相关问题