instance method match

Ruby on Rails 8.1.4

Since v3.0.20

Available in: v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.11.3 v5.0.7.2 v5.1.7 v5.2.8.1 v6.0.6.1 v6.1.7.10 v7.0.10 v7.1.6 v7.2.4 v8.0.5.1 v8.1.4 edge

Signature

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_actions rest
as key = DEFAULT
via key = nil
to key = nil
controller key = nil
action key = nil
on key = nil
defaults key = nil
constraints key = nil
anchor key = nil
format key = nil
path key = nil
internal key = nil
mapping keyrest
block block
Source
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1837
        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 1837 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionDispatch::Routing::Mapper::Resources

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close