修复匹配路线的 rails 5 弃用警告 [英] Fixing rails 5 deprecation warning for matching routes
问题描述
在我的 routes.rb
文件中,我有以下内容
In my routes.rb
file, I have the following
resources :landings, only: [:index],
path: '/golf' do
collection do
get 'break_70', path: 'how-to-break-70'
end
end
生成一个url
/golf/how-to-break-70
升级到 Rails 5 后,这会生成以下弃用消息:
After upgrading to Rails 5, this generates the following deprecation message:
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"
如果我尝试按照这些指示
If I try to follow these directions with
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'
如何修改此路由以避免出现弃用警告?
How do I modify this route to avoid the deprecation warning?
推荐答案
更新答案
我更新了我的答案以修复一个小错误,以便任何读者都可以看到工作代码.感谢@Obromios 的小修复.
Updated Answer
I update my answer to fix a little mistake so any reader can see a working code. Thanks @Obromios for the little fix.
在resources
中它仍然应该是path:
.在路由定义中,match
后面多了一个:".如果您match
,您还必须指定via: [:get, :post]
.
所以最终版本看起来像:
In resources
it should still be path:
.
And in the route definition you have an extra ":" after match
. Also you have to specify via: [:get, :post]
if you match
.
So the final version looks like:
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
之后有一个额外的:"所以最终版本看起来像:
In resources
it should still be path:
.
And in the route definition you have an extra ":" after match
So the final version looks like:
resources :landings, only: [:index], path: '/golf' do
collection do
match 'how-to-break-70', as: 'break_70', action: 'break_70'
end
end
未经测试,但应该可以工作.
Not tested, but should work.
这篇关于修复匹配路线的 rails 5 弃用警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!