module ActionDispatch::Routing::Mapper::Base
def match(path, options = nil)
Allows you to specify the default value for optional +format+
[:format]
match 'path', to: 'c#a', anchor: false, via: :get
# 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' }, via: :get
# 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: PermitList.new, via: :get
end
def matches?(request) request.remote_ip == '1.2.3.4' end
class PermitList
match 'json_only', constraints: { format: 'json' }, via: :get
match 'path/:id', constraints: { id: /[A-Z]\d{5}/ }, via: :get
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, via: :get
match 'path', to: -> (env) { [200, {}, ["Success!"]] }, via: :get
match 'path', to: 'controller#action', via: :get
+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', via: :get
The namespace for :controller.
[:module]
The path prefix for the routes.
[:path]
user_path(user) # => "/users/Phusion"
user = User.find_by(name: 'Phusion')
end
end
name
def to_param
class User < ActiveRecord::Base
model to construct a URL:
You can override ActiveRecord::Base#to_param of a related
DELETE /users/:name(.:format)
PATCH/PUT /users/:name(.:format)
GET /users/:name(.:format)
GET /users/:name/edit(.:format)
GET /users/new(.:format)
POST /users(.:format)
GET /users(.:format)
The +users+ resource here will have the following routes generated for it:
resources :users, param: :name
In your router:
params[<:param>].
You can access that segment from your controller using
dynamic segment used to generate the routes).
Overrides the default resource identifier +:id+ (name of the
[:param]
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+
the via options or use one of the HttpHelpers[rdoc-ref:HttpHelpers]
implications, you must either specify the actions in
Because requesting various HTTP verbs with a single action has security
match 'photos/:id', to: PhotosController.action(:show), via: :get
# Yes, controller actions are just rack endpoints
match 'photos/:id', to: PhotoRackApp, via: :get
match 'photos/:id', to: -> (hash) { [200, {}, ["Coming soon"]] }, via: :get
responds to +call+:
A pattern can also point to a +Rack+ endpoint i.e. anything that
match 'photos/:id', controller: 'photos', action: 'show', via: :get
match 'photos/:id', to: 'photos#show', via: :get
match 'photos/:id' => 'photos#show', via: :get
+:controller+ should be set in options or hash shorthand. Examples:
When a pattern points to an internal route, the route's +:action+ and
can't be parsed.
Without a variable name to attach the glob parameter to, the route
To match a wildcard parameter, it must have a name assigned to it.
# params[:title] = 'stairway-to-heaven'
# params[:category] = 'rock/classic'
# 'songs/rock/classic/stairway-to-heaven' sets
get '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
get ":controller/:action/:id"
Do:
match ":controller/:action/:id"
Instead of:
If you want to expose your action to GET, use +get+ in the router:
query parameters and thus available through +params+ in an action.
Note that +:controller+, +:action+ and +:id+ are interpreted as URL
match ':controller/:action/:id', via: [:get, :post]
# sets :controller, :action and :id in params
If you want to expose your action to both GET and POST, use:
without specifying an HTTP method.
You should not use the +match+ method in your router
Matches a URL pattern to one or more routes.
def match(path, options = nil) end