instance method
mount
Ruby on Rails 8.1.2
Since v3.0.20Signature
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
-
appopt = nil -
deprecated_optionsopt = nil -
askey = DEFAULT -
viakey = nil -
atkey = nil -
defaultskey = nil -
constraintskey = nil -
anchorkey = false -
formatkey = false -
pathkey = nil -
internalkey = nil -
mappingkeyrest -
blockblock
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