instance method mount

Ruby on Rails 8.1.2

Since v3.0.20

Available in: v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

mount(app = nil, deprecated_options = nil, as: DEFAULT, via: nil, at: nil, defaults: nil, constraints: nil, anchor: false, format: false, path: nil, internal: nil, **mapping, &block)

Mount a Rack-based application to be used within the application.

mount SomeRackApp, at: "some_route"

For options, see match, as mount uses it internally.

All mounted applications come with routing helpers to access them. These are named after the class specified, so for the above example the helper is either some_rack_app_path or some_rack_app_url. To customize this helper’s name, use the :as option:

mount(SomeRackApp, at: "some_route", as: "exciting")

This will generate the exciting_path and exciting_url helpers which can be used to navigate to this mounted app.

Parameters

app opt = nil
deprecated_options opt = nil
as key = DEFAULT
via key = nil
at key = nil
defaults key = nil
constraints key = nil
anchor key = false
format key = false
path key = nil
internal key = nil
mapping keyrest
block block
Source
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 605
        def mount(app = nil, deprecated_options = nil, as: DEFAULT, via: nil, at: nil, defaults: nil, constraints: nil, anchor: false, format: false, path: nil, internal: nil, **mapping, &block)
          if deprecated_options.is_a?(Hash)
            as = assign_deprecated_option(deprecated_options, :as, :mount) if deprecated_options.key?(:as)
            via ||= assign_deprecated_option(deprecated_options, :via, :mount)
            at ||= assign_deprecated_option(deprecated_options, :at, :mount)
            defaults ||= assign_deprecated_option(deprecated_options, :defaults, :mount)
            constraints ||= assign_deprecated_option(deprecated_options, :constraints, :mount)
            anchor = assign_deprecated_option(deprecated_options, :anchor, :mount) if deprecated_options.key?(:anchor)
            format = assign_deprecated_option(deprecated_options, :format, :mount) if deprecated_options.key?(:format)
            path ||= assign_deprecated_option(deprecated_options, :path, :mount)
            internal ||= assign_deprecated_option(deprecated_options, :internal, :mount)
            assign_deprecated_options(deprecated_options, mapping, :mount)
          end

          path_or_action = at

          if app.nil?
            hash_app, hash_path = mapping.find { |key, _| key.respond_to?(:call) }
            mapping.delete(hash_app) if hash_app

            app ||= hash_app
            path_or_action ||= hash_path
          end

          raise ArgumentError, "A rack application must be specified" unless app.respond_to?(:call)
          raise ArgumentError, <<~MSG unless path_or_action
            Must be called with mount point

              mount SomeRackApp, at: "some_route"
              or
              mount(SomeRackApp => "some_route")
          MSG

          rails_app = rails_app? app
          as = app_name(app, rails_app) if as == DEFAULT

          target_as = name_for_action(as, path_or_action)
          via ||= :all

          match(path_or_action, to: app, as:, via:, defaults:, constraints:, anchor:, format:, path:, internal:, **mapping, &block)

          define_generate_prefix(app, target_as) if rails_app
          self
        end

Defined in actionpack/lib/action_dispatch/routing/mapper.rb line 605 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionDispatch::Routing::Mapper::Base

Type at least 2 characters to search.

↑↓ navigate · open · esc close