Method: ActionDispatch::Routing::Mapper::Base#mount

Defined in:
lib/action_dispatch/routing/mapper.rb

#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) ⇒ Object

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.

Raises:

  • (ArgumentError)


605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
# File '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, "    Must be called with mount point\n\n      mount SomeRackApp, at: \"some_route\"\n      or\n      mount(SomeRackApp => \"some_route\")\n  MSG\n\n  rails_app = rails_app? app\n  as = app_name(app, rails_app) if as == DEFAULT\n\n  target_as = name_for_action(as, path_or_action)\n  via ||= :all\n\n  match(path_or_action, to: app, as:, via:, defaults:, constraints:, anchor:, format:, path:, internal:, **mapping, &block)\n\n  define_generate_prefix(app, target_as) if rails_app\n  self\nend\n" unless path_or_action