【发布时间】:2020-04-15 21:50:58
【问题描述】:
使用有问题吗
获取“注册”=>“用户#索引”
而不是
get 'signup', to: 'users#index'?
【问题讨论】:
标签: ruby-on-rails routes
使用有问题吗
获取“注册”=>“用户#索引”
而不是
get 'signup', to: 'users#index'?
【问题讨论】:
标签: ruby-on-rails routes
TL;DR
没有。
并非如此,get 方法已准备好处理其中任何一个。
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 711
def get(*args, &block)
map_method(:get, args, &block)
end
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 748
def map_method(method, args, &block)
options = args.extract_options!
options[:via] = method
match(*args, options, &block)
self
end
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1597
#
# Matches a URL pattern to one or more routes.
# For more information, see match[rdoc-ref:Base#match].
#
# match 'path' => 'controller#action', via: :patch
# match 'path', to: 'controller#action', via: :post
# match 'path', 'otherpath', on: :member, via: :get
def match(path, *rest, &block)
在一天结束时,match 被调用,如果你去查看它的实现,它有正确处理它的代码。
另外,The Rails Style Guide 对此没有任何建议。
【讨论】:
这两种方式都不应该有问题。 =>,称为hashrocket,是, to: 的简写。
【讨论】: