Class | Merb::Router::Behavior |
In: |
lib/merb-core/dispatch/router/behavior.rb
lib/merb-core/dispatch/router/resources.rb |
Parent: | Object |
Capture any new routes that have been added within the block.
This utility method lets you track routes that have been added; it doesn‘t affect how/which routes are added.
&block: | A context in which routes are generated. |
:api: public
Sets default values for route parameters. If no value for the key can be extracted from the request, then the value provided here will be used.
defaults<Hash>: | The default values for named segments. |
&block: | All routes defined in the block will be scoped to the defaults defined by the default method. |
r<Behavior>: | optional - The defaults behavior object. |
:api: public
Creates the most common routes /:controller/:action/:id.format when called with no arguments. You can pass a hash or a block to add parameters or override the default behavior.
params<Hash>: | This optional hash can be used to augment the default settings |
&block: | When passing a block a new behavior is yielded and more refinement is possible. |
Route: | the default route |
# Passing an extra parameter "mode" to all matches r.default_routes :mode => "default" # specifying exceptions within a block r.default_routes do |nr| nr.defer_to do |request, params| nr.match(:protocol => "http://").to(:controller => "login", :action => "new") if request.env["REQUEST_URI"] =~ /\/private\// end end
:api: public
Takes a Proc as a parameter and applies it as a deferred proc for all the routes defined in the block. This is mostly interesting for plugin developers.
defered_block = proc do |r, p| p.merge :controller => 'api/comments' if request.xhr? end defer(defered_block) do resources :comments end
:api: public
Takes a block and stores it for deferred conditional routes. The block takes the request object and the params hash as parameters.
params<Hash>: | Parameters and conditions associated with this behavior. |
&conditional_block: | A block with the conditions to be met for the behavior to take effect. |
Route : | The default route. |
The block takes two parameters, request and params. The params that are passed into the block are just the placeholder params from the route. If you want the full parsed params, use request.params.
The rationale for this is that request.params is a fairly slow operation, and if the full params parsing is not required, we would rather not do the full parsing.
defer_to do |request, params| params.merge :controller => 'here', :action => 'there' if request.xhr? end
:api: public
Specifies that a route can be fixatable.
enabled<Boolean>: | True enables fixation on the route. |
:api: public
Sets a method for instances of specified Classes to be called before insertion into a route. This is useful when using models and want a specific method to be called on it (For example, for ActiveRecord::Base it would be to_param).
The default method called on objects is to_s.
identifiers<Hash>: | The keys are Classes and the values are the method that instances of the specified class should have called on. |
&block: | All routes defined in the block will be call the specified methods during generation. |
r<Behavior>: | The identify behavior object. This is optional |
:api: public
Defines the conditions that are required to match a Request. Each condition is applied to a method of the Request object. Conditions can also be applied to segments of the path.
If match is passed a block, it will create a new route scope with the conditions passed to it and yield to the block such that all routes that are defined in the block have the conditions applied to them.
path<String, Regexp>: | The pattern against which Merb::Request path
is matched.
When path is a String, any substring that is wrapped in parenthesis is considered optional and any segment that begins with a colon, ex.: ":login", defines both a capture and a named param. Extra conditions can then be applied each named param individually. When path is a Regexp, the pattern is left untouched and the Merb::Request path is matched against it as is. path is optional. |
conditions<Hash>: | Additional conditions that the request must meet in order to match. The keys must be the names of previously defined path segments or be methods that the Merb::Request instance will respond to. The value is the string or regexp that matched the returned value. Conditions are inherited by child routes. |
&block: | All routes defined in the block will be scoped to the conditions defined by the match method. |
r<Behavior>: | optional - The match behavior object. |
Behavior: | A new instance of Behavior with the specified path and conditions. |
Tip: When nesting always make sure the most inner sub-match registers a Route and doesn‘t just return new Behaviors.
# registers /foo/bar to controller => "foo", :action => "bar" # and /foo/baz to controller => "foo", :action => "baz" match("/foo") do match("/bar").to(:controller => "foo", :action => "bar") match("/baz").to(:controller => "foo", :action => "caz") end # Checks the format of the segments against the specified Regexp match("/:string/:number", :string => /[a-z]+/, :number => /\d+/). to(:controller => "string_or_numbers") # Equivalent to the default_route match("/:controller(/:action(:id))(.:format)").register #match only if the browser string contains MSIE or Gecko match("/foo", :user_agent => /(MSIE|Gecko)/ ) .to(:controller => 'foo', :action => 'popular') # Route GET and POST requests to different actions (see also #resources) r.match('/foo', :method => :get).to(:action => 'show') r.match('/foo', :method => :post).to(:action => 'create') # match also takes regular expressions r.match(%r[/account/([a-z]{4,6})]).to(:controller => "account", :action => "show", :id => "[1]") r.match(%r{/?(en|es|fr|be|nl)?}).to(:language => "[1]") do match("/guides/:action/:id").to(:controller => "tour_guides") end
:api: public
Creates a namespace for a route. This way you can have logical separation to your routes.
name_or_path<String, Symbol>: | The name or path of the namespace. |
options<Hash>: | Optional hash (see below) |
&block: | All routes defined in the block will be scoped to the namespace defined by the namespace method. |
:path<String>: | match against this url |
r<Behavior>: | The namespace behavior object. This is optional |
namespace :admin do resources :accounts resource :email end # /super_admin/accounts namespace(:admin, :path=>"super_admin") do resources :accounts end
:api: public
Allows the fine tuning of certain router options.
options<Hash>: | The options to set for all routes defined in the scope. The currently supported options are: |
&block: | All routes defined in the block will be scoped to the options defined by the options method. |
r<Behavior>: | The options behavior object. This is optional |
# If :group is not matched in the path, it will be "registered" instead # of nil. match("/users(/:group)").default(:group => "registered")
:api: public
Creates a Route from one or more Behavior objects, unless a block is passed in.
params<Hash>: | The parameters the route maps to. |
&block: | All routes defined in the block will be scoped to the params defined by the to method. |
r<Behavior>: | optional - The to behavior object. |
Behavior: | The route definition behavior defining the created route |
match('/:controller/:id).to(:action => 'show') to(:controller => 'simple') do match('/test').to(:action => 'index') match('/other').to(:action => 'other') end
:api: public