Module: FakeRails3Routes::Mapper::Base
- Included in:
- FakeRails3Routes::Mapper
- Defined in:
- lib/fake_rails3_routes/mapper.rb
Instance Method Summary collapse
- #default_url_options=(options) ⇒ Object (also: #default_url_options)
-
#match(path, options = nil) ⇒ Object
Matches a url pattern to one or more routes.
-
#root(options = {}) ⇒ Object
You can specify what Rails should route “/” to with the root method:.
- #with_default_scope(scope, &block) ⇒ Object
Instance Method Details
#default_url_options=(options) ⇒ Object Also known as: default_url_options
332 333 334 |
# File 'lib/fake_rails3_routes/mapper.rb', line 332 def () @set. = 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' => '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)
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' => 'c#a', :module => 'sekret', :controller => 'posts' #=> Sekret::PostsControllerSee
Scoping#namespacefor its scope equivalent. - :as
-
The name used to generate routing helpers.
- :via
-
Allowed HTTP verb(s) for route.
match 'path' => 'c#a', :via => :get match 'path' => 'c#a', :via => [:get, :post] - :to
-
Points to a
Rackendpoint. Can be an object that responds tocallor 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 withinresource(s)block. For example:resource :bar do match 'foo' => 'c#a', :on => :member, :via => [:get, :post] endIs equivalent to:
resource :bar do member do match 'foo' => 'c#a', :via => [:get, :post] end end - :constraints
-
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.newSee
Scoping#constraintsfor more examples with its scope equivalent. - :defaults
-
Sets defaults for parameters
# Sets params[:format] to 'jpg' by default match 'path' => 'c#a', :defaults => { :format => 'jpg' }See
Scoping#defaultsfor its scope equivalent. - :anchor
-
Boolean to anchor a
matchpattern. 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
329 330 |
# File 'lib/fake_rails3_routes/mapper.rb', line 329 def match(path, =nil) 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 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.
210 211 212 |
# File 'lib/fake_rails3_routes/mapper.rb', line 210 def root( = {}) match '/', { :as => :root }.merge() end |
#with_default_scope(scope, &block) ⇒ Object
337 338 339 340 341 |
# File 'lib/fake_rails3_routes/mapper.rb', line 337 def with_default_scope(scope, &block) scope(scope) do instance_exec(&block) end end |