match(path, *rest)
public
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
Show source
def match(path, *rest)
if rest.empty? && Hash === path
options = path
path, to = options.find { |name, _value| name.is_a?(String) }
case to
when Symbol
options[:action] = to
when String
if to =~ /#/
options[:to] = to
else
options[:controller] = to
end
else
options[:to] = to
end
options.delete(path)
paths = [path]
else
options = rest.pop || {}
paths = [path] + rest
end
if options[:on] && !VALID_ON_OPTIONS.include?(options[:on])
raise ArgumentError, "Unknown scope #{on.inspect} given to :on"
end
if @scope[:to]
options[:to] ||= @scope[:to]
end
if @scope[:controller] && @scope[:action]
options[:to] ||= "#{@scope[:controller]}##{@scope[:action]}"
end
controller = options.delete(:controller) || @scope[:controller]
option_path = options.delete :path
to = options.delete :to
via = Mapping.check_via Array(options.delete(:via) {
@scope[:via]
})
formatted = options.delete(:format) { @scope[:format] }
anchor = options.delete(:anchor) { true }
options_constraints = options.delete(:constraints) || {}
path_types = paths.group_by(&:class)
path_types.fetch(String, []).each do |_path|
route_options = options.dup
if _path && option_path
ActiveSupport::Deprecation.warn Specifying strings for both :path and the route path is deprecated. Change things like this: match
route_options[:action] = _path
route_options[:as] = _path
_path = option_path
end
to = get_to_from_path(_path, to, route_options[:action])
decomposed_match(_path, controller, route_options, _path, to, via, formatted, anchor, options_constraints)
end
path_types.fetch(Symbol, []).each do |action|
route_options = options.dup
decomposed_match(action, controller, route_options, option_path, to, via, formatted, anchor, options_constraints)
end
self
end