instance method
match
Ruby on Rails edge
Since v3.0.20Signature
match(*path_or_actions, as: DEFAULT, via: nil, to: nil, controller: nil, action: nil, on: nil, defaults: nil, constraints: nil, anchor: nil, format: nil, path: nil, internal: nil, **mapping, &block)
Matches a URL pattern to one or more routes. For more information, see match.
match 'path', to: 'controller#action', via: :post
match 'otherpath', on: :member, via: :get
Parameters
-
path_or_actionsrest -
askey = DEFAULT -
viakey = nil -
tokey = nil -
controllerkey = nil -
actionkey = nil -
onkey = nil -
defaultskey = nil -
constraintskey = nil -
anchorkey = nil -
formatkey = nil -
pathkey = nil -
internalkey = nil -
mappingkeyrest -
blockblock
Source
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1848
def match(*path_or_actions, as: DEFAULT, via: nil, to: nil, controller: nil, action: nil, on: nil, defaults: nil, constraints: nil, anchor: nil, format: nil, path: nil, internal: nil, **mapping, &block)
if path_or_actions.grep(Hash).any? && (deprecated_options = path_or_actions.extract_options!)
as = assign_deprecated_option(deprecated_options, :as, :match) if deprecated_options.key?(:as)
via ||= assign_deprecated_option(deprecated_options, :via, :match)
to ||= assign_deprecated_option(deprecated_options, :to, :match)
controller ||= assign_deprecated_option(deprecated_options, :controller, :match)
action ||= assign_deprecated_option(deprecated_options, :action, :match)
on ||= assign_deprecated_option(deprecated_options, :on, :match)
defaults ||= assign_deprecated_option(deprecated_options, :defaults, :match)
constraints ||= assign_deprecated_option(deprecated_options, :constraints, :match)
anchor = assign_deprecated_option(deprecated_options, :anchor, :match) if deprecated_options.key?(:anchor)
format = assign_deprecated_option(deprecated_options, :format, :match) if deprecated_options.key?(:format)
path ||= assign_deprecated_option(deprecated_options, :path, :match)
internal ||= assign_deprecated_option(deprecated_options, :internal, :match)
assign_deprecated_options(deprecated_options, mapping, :match)
end
raise ArgumentError, "Wrong number of arguments (expect 1, got #{path_or_actions.count})" if path_or_actions.count > 1
if path_or_actions.none? && mapping.any?
hash_path, hash_to = mapping.find { |key, _| key.is_a?(String) }
if hash_path.nil?
raise ArgumentError, "Route path not specified"
else
mapping.delete(hash_path)
end
if hash_path
path_or_actions.push hash_path
case hash_to
when Symbol
action ||= hash_to
when String
if hash_to.include?("#")
to ||= hash_to
else
controller ||= hash_to
end
else
to ||= hash_to
end
end
end
path_or_actions.each do |path_or_action|
if defaults
defaults(defaults) { map_match(path_or_action, as:, via:, to:, controller:, action:, on:, constraints:, anchor:, format:, path:, internal:, mapping:, &block) }
else
map_match(path_or_action, as:, via:, to:, controller:, action:, on:, constraints:, anchor:, format:, path:, internal:, mapping:, &block)
end
end
end
Defined in actionpack/lib/action_dispatch/routing/mapper.rb line 1848
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActionDispatch::Routing::Mapper::Resources