Module: ActionDispatch::Routing::Mapper::Base

Included in:
ActionDispatch::Routing::Mapper
Defined in:
lib/action_dispatch/routing/mapper.rb

Instance Method Summary collapse

Instance Method Details

#default_url_options=(options) ⇒ Object Also known as: default_url_options



507
508
509
# File 'lib/action_dispatch/routing/mapper.rb', line 507

def default_url_options=(options)
  @set.default_url_options = options
end

#has_named_route?(name) ⇒ Boolean

Query if the following named route was already defined.

Returns:

  • (Boolean)


519
520
521
# File 'lib/action_dispatch/routing/mapper.rb', line 519

def has_named_route?(name)
  @set.named_routes.routes[name.to_sym]
end

#match(path, options = nil) ⇒ Object

Matches a url pattern to one or more routes. Any symbols in a pattern are interpreted as url query parameters and thus available as params in an action:

# sets :controller, :action and :id in params
match ':controller/:action/:id'

Two of these symbols are special, :controller maps to the controller and :action to the controller’s action. A pattern can also map wildcard segments (globs) to params:

match 'songs/*category/:title', to: 'songs#show'

# 'songs/rock/classic/stairway-to-heaven' sets
#  params[:category] = 'rock/classic'
#  params[:title] = 'stairway-to-heaven'

When a pattern points to an internal route, the route’s :action and :controller should be set in options or hash shorthand. Examples:

match 'photos/:id' => 'photos#show'
match 'photos/:id', to: 'photos#show'
match 'photos/:id', controller: 'photos', action: 'show'

A pattern can also point to a Rack endpoint i.e. anything that responds to call:

match 'photos/:id', to: lambda {|hash| [200, {}, ["Coming soon"]] }
match 'photos/:id', to: PhotoRackApp
# Yes, controller actions are just rack endpoints
match 'photos/:id', to: PhotosController.action(:show)

Because request various HTTP verbs with a single action has security implications, is recommendable use HttpHelpers instead match

Options

Any options not seen here are passed on as params with the url.

:controller

The route’s controller.

:action

The route’s action.

:path

The path prefix for the routes.

:module

The namespace for :controller.

match 'path', to: 'c#a', module: 'sekret', controller: 'posts'
#=> Sekret::PostsController

See Scoping#namespace for its scope equivalent.

:as

The name used to generate routing helpers.

:via

Allowed HTTP verb(s) for route.

match 'path', to: 'c#a', via: :get
match 'path', to: 'c#a', via: [:get, :post]
match 'path', to: 'c#a', via: :all
:to

Points to a Rack endpoint. Can be an object that responds to call or a string representing a controller’s action.

match 'path', to: 'controller#action'
match 'path', to: lambda { |env| [200, {}, ["Success!"]] }
match 'path', to: RackApp
:on

Shorthand for wrapping routes in a specific RESTful context. Valid values are :member, :collection, and :new. Only use within resource(s) block. For example:

resource :bar do
  match 'foo', to: 'c#a', on: :member, via: [:get, :post]
end

Is equivalent to:

resource :bar do
  member do
    match 'foo', to: 'c#a', via: [:get, :post]
  end
end
:constraints

Constrains parameters with a hash of regular expressions or an object that responds to matches?. In addition, constraints other than path can also be specified with any object that responds to === (eg. String, Array, Range, etc.).

match 'path/:id', constraints: { id: /[A-Z]\d{5}/ }

match 'json_only', constraints: { format: 'json' }

class Blacklist
  def matches?(request) request.remote_ip == '1.2.3.4' end
end
match 'path', to: 'c#a', constraints: Blacklist.new

See Scoping#constraints for more examples with its scope equivalent.

:defaults

Sets defaults for parameters

# Sets params[:format] to 'jpg' by default
match 'path', to: 'c#a', defaults: { format: 'jpg' }

See Scoping#defaults for its scope equivalent.

:anchor

Boolean to anchor a match pattern. Default is true. When set to false, the pattern matches any request prefixed with the given path.

# Matches any request starting with 'path'
match 'path', to: 'c#a', anchor: false
:format

Allows you to specify the default value for optional format segment or disable it by supplying false.



461
462
# File 'lib/action_dispatch/routing/mapper.rb', line 461

def match(path, options=nil)
end

#mount(app, options = nil) ⇒ Object

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

mount SomeRackApp, at: "some_route"

Alternatively:

mount(SomeRackApp => "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 => "some_route", as: "exciting")

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



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/action_dispatch/routing/mapper.rb', line 483

def mount(app, options = nil)
  if options
    path = options.delete(:at)
  else
    unless Hash === app
      raise ArgumentError, "must be called with mount point"
    end

    options = app
    app, path = options.find { |k, _| k.respond_to?(:call) }
    options.delete(app) if app
  end

  raise "A rack application must be specified" unless path

  options[:as]  ||= app_name(app)
  options[:via] ||= :all

  match(path, options.merge(:to => app, :anchor => false, :format => false))

  define_generate_prefix(app, options[:as])
  self
end

#root(options = {}) ⇒ Object

You can specify what Rails should route “/” to with the root method:

root to: 'pages#main'

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

You can also pass a string which will expand

root 'pages#main'

You should put the root route at the top of config/routes.rb, because this means it will be matched first. As this is the most popular route of most Rails applications, this is beneficial.



329
330
331
# File 'lib/action_dispatch/routing/mapper.rb', line 329

def root(options = {})
  match '/', { :as => :root, :via => :get }.merge!(options)
end

#with_default_scope(scope, &block) ⇒ Object



512
513
514
515
516
# File 'lib/action_dispatch/routing/mapper.rb', line 512

def with_default_scope(scope, &block)
  scope(scope) do
    instance_exec(&block)
  end
end