module ActionDispatch::Routing::Mapper::Base
def app_name(app)
def app_name(app) return unless app.respond_to?(:routes) if app.respond_to?(:railtie_name) app.railtie_name else class_name = app.class.is_a?(Class) ? app.name : app.class.name ActiveSupport::Inflector.underscore(class_name).tr("/", "_") end end
def default_url_options=(options)
def default_url_options=(options) @set.default_url_options = options end
def define_generate_prefix(app, name)
def define_generate_prefix(app, name) return unless app.respond_to?(:routes) && app.routes.respond_to?(:define_mounted_helper) _route = @set.named_routes.routes[name.to_sym] _routes = @set app.routes.define_mounted_helper(name) app.routes.singleton_class.class_eval do define_method :mounted? do true end define_method :_generate_prefix do |options| prefix_options = options.slice(*_route.segment_keys) # we must actually delete prefix segment keys to avoid passing them to next url_for _route.segment_keys.each { |k| options.delete(k) } _routes.url_helpers.send("#{name}_path", prefix_options) end end end
def has_named_route?(name)
def has_named_route?(name) @set.named_routes.routes[name.to_sym] end
def match(path, options=nil)
Allows you to specify the default value for optional +format+
[:format]
match 'path', to: 'c#a', anchor: false
# Matches any request starting with 'path'
false, the pattern matches any request prefixed with the given path.
Boolean to anchor a match pattern. Default is true. When set to
[:anchor]
See Scoping#defaults for its scope equivalent.
match 'path', to: 'c#a', defaults: { format: 'jpg' }
# Sets params[:format] to 'jpg' by default
Sets defaults for parameters
[:defaults]
equivalent.
See Scoping#constraints for more examples with its scope
match 'path', to: 'c#a', constraints: Blacklist.new
end
def matches?(request) request.remote_ip == '1.2.3.4' end
class Blacklist
match 'json_only', constraints: { format: 'json' }
match 'path/:id', constraints: { id: /[A-Z]\d{5}/ }
that responds to === (eg. String, Array, Range, etc.).
other than path can also be specified with any object
or an object that responds to matches?. In addition, constraints
Constrains parameters with a hash of regular expressions
[:constraints]
end
end
match 'foo', to: 'c#a', via: [:get, :post]
member do
resource :bar do
Is equivalent to:
end
match 'foo', to: 'c#a', on: :member, via: [:get, :post]
resource :bar do
resource(s) block. For example:
values are +:member+, +:collection+, and +:new+. Only use within
Shorthand for wrapping routes in a specific RESTful context. Valid
[:on]
match 'path', to: RackApp
match 'path', to: lambda { |env| [200, {}, ["Success!"]] }
match 'path', to: 'controller#action'
+call+ or a string representing a controller's action.
Points to a +Rack+ endpoint. Can be an object that responds to
[:to]
match 'path', to: 'c#a', via: :all
match 'path', to: 'c#a', via: [:get, :post]
match 'path', to: 'c#a', via: :get
Allowed HTTP verb(s) for route.
[:via]
The name used to generate routing helpers.
[:as]
See Scoping#namespace for its scope equivalent.
#=> Sekret::PostsController
match 'path', to: 'c#a', module: 'sekret', controller: 'posts'
The namespace for :controller.
[:module]
The path prefix for the routes.
[:path]
The route's action.
[:action]
The route's controller.
[:controller]
Any options not seen here are passed on as params with the url.
=== Options
instead +match+
implications, is recommendable use HttpHelpers[rdoc-ref:HttpHelpers]
Because request various HTTP verbs with a single action has security
match 'photos/:id', to: PhotosController.action(:show)
# Yes, controller actions are just rack endpoints
match 'photos/:id', to: PhotoRackApp
match 'photos/:id', to: lambda {|hash| [200, {}, ["Coming soon"]] }
responds to +call+:
A pattern can also point to a +Rack+ endpoint i.e. anything that
match 'photos/:id', controller: 'photos', action: 'show'
match 'photos/:id', to: 'photos#show'
match 'photos/:id' => 'photos#show'
+:controller+ should be set in options or hash shorthand. Examples:
When a pattern points to an internal route, the route's +:action+ and
# params[:title] = 'stairway-to-heaven'
# params[:category] = 'rock/classic'
# 'songs/rock/classic/stairway-to-heaven' sets
match 'songs/*category/:title', to: 'songs#show'
wildcard segments (globs) to params:
and +:action+ to the controller's action. A pattern can also map
Two of these symbols are special, +:controller+ maps to the controller
match ':controller/:action/:id'
# sets :controller, :action and :id in params
in an action:
are interpreted as url query parameters and thus available as +params+
Matches a url pattern to one or more routes. Any symbols in a pattern
def match(path, options=nil) end
def mount(app, options = nil)
This will generate the +exciting_path+ and +exciting_url+ helpers
mount(SomeRackApp => "some_route", as: "exciting")
To customize this helper's name, use the +:as+ option:
the helper is either +some_rack_app_path+ or +some_rack_app_url+.
These are named after the class specified, so for the above example
All mounted applications come with routing helpers to access them.
For options, see +match+, as +mount+ uses it internally.
mount(SomeRackApp => "some_route")
Alternatively:
mount SomeRackApp, at: "some_route"
Mount a Rack-based application to be used within the application.
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
def root(options = {})
because this means it will be matched first. As this is the most popular route
You should put the root route at the top of config/routes.rb,
root 'pages#main'
You can also pass a string which will expand
For options, see +match+, as +root+ uses it internally.
root to: 'pages#main'
You can specify what Rails should route "/" to with the root method:
def root(options = {}) match '/', { :as => :root, :via => :get }.merge!(options) end
def with_default_scope(scope, &block)
def with_default_scope(scope, &block) scope(scope) do instance_exec(&block) end end