# File lib/action_dispatch/routing/mapper.rb, line 419 419: def default_url_options=(options) 420: @set.default_url_options = options 421: end
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' => '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' => lambda {|hash| [200, {}, "Coming soon"] } match 'photos/:id' => PhotoRackApp # Yes, controller actions are just rack endpoints match 'photos/:id' => PhotosController.action(:show)
Any options not seen here are passed on as params with the url.
The route’s controller.
The route’s action.
The path prefix for the routes.
The namespace for :controller.
match 'path' => 'c#a', :module => 'sekret', :controller => 'posts' #=> Sekret::PostsController
See Scoping#namespace for its scope equivalent.
The name used to generate routing helpers.
Allowed HTTP verb(s) for route.
match 'path' => 'c#a', :via => :get match 'path' => 'c#a', :via => [:get, :post]
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
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' => 'c#a', :on => :member, :via => [:get, :post] end
Is equivalent to:
resource :bar do member do match 'foo' => 'c#a', :via => [:get, :post] end end
Constrains parameters with a hash of regular expressions or an object that responds to matches?
match 'path/:id', :constraints => { :id => /[A-Z]\d{5}/ } class Blacklist def matches?(request) request.remote_ip == '1.2.3.4' end end match 'path' => 'c#a', :constraints => Blacklist.new
See Scoping#constraints for more examples with its scope equivalent.
Sets defaults for parameters
# Sets params[:format] to 'jpg' by default match 'path' => 'c#a', :defaults => { :format => 'jpg' }
See Scoping#defaults for its scope equivalent.
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' => 'c#a', :anchor => false
# File lib/action_dispatch/routing/mapper.rb, line 378 378: def match(path, options=nil) 379: end
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.
# File lib/action_dispatch/routing/mapper.rb, line 400 400: def mount(app, options = nil) 401: if options 402: path = options.delete(:at) 403: else 404: options = app 405: app, path = options.find { |k, v| k.respond_to?(:call) } 406: options.delete(app) if app 407: end 408: 409: raise "A rack application must be specified" unless path 410: 411: options[:as] ||= app_name(app) 412: 413: match(path, options.merge(:to => app, :anchor => false, :format => false)) 414: 415: define_generate_prefix(app, options[:as]) 416: self 417: end
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 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.
# File lib/action_dispatch/routing/mapper.rb, line 259 259: def root(options = {}) 260: match '/', { :as => :root }.merge(options) 261: end
# File lib/action_dispatch/routing/mapper.rb, line 431 431: def app_name(app) 432: return unless app.respond_to?(:routes) 433: 434: if app.respond_to?(:railtie_name) 435: app.railtie_name 436: else 437: class_name = app.class.is_a?(Class) ? app.name : app.class.name 438: ActiveSupport::Inflector.underscore(class_name).gsub("/", "_") 439: end 440: end
# File lib/action_dispatch/routing/mapper.rb, line 442 442: def define_generate_prefix(app, name) 443: return unless app.respond_to?(:routes) && app.routes.respond_to?(:define_mounted_helper) 444: 445: _route = @set.named_routes.routes[name.to_sym] 446: _routes = @set 447: app.routes.define_mounted_helper(name) 448: app.routes.class_eval do 449: define_method :_generate_prefix do |options| 450: prefix_options = options.slice(*_route.segment_keys) 451: # we must actually delete prefix segment keys to avoid passing them to next url_for 452: _route.segment_keys.each { |k| options.delete(k) } 453: prefix = _routes.url_helpers.send("#{name}_path", prefix_options) 454: prefix = '' if prefix == '/' 455: prefix 456: end 457: end 458: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.