Class | Merb::Router::Behavior |
In: |
lib/merb-core/dispatch/router/behavior.rb
|
Parent: | Object |
conditions | [R] | |
params | [R] | |
parent | [RW] | |
placeholders | [R] |
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
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. |
r.defer_to do |request, params| params.merge :controller => 'here', :action => 'there' if request.xhr? end
Matches a path and any number of optional request methods as conditions of a route. Alternatively, path can be a hash of conditions, in which case conditions ignored.
path<String, Regexp>: | When passing a string as path you‘re defining a literal definition for your route. Using a colon, ex.: ":login", defines both a capture and a named param. When passing a regular expression you can define captures explicitly within the regular expression syntax. path is optional. |
conditions<Hash>: | This optional hash helps refine the settings for the route. When combined with a block it can help keep your routes DRY |
&block: | Passes a new instance of a Behavior object into the optional block so that sub-matching and routes nesting may occur. |
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 returns new Behaviors.
# registers /foo/bar to controller => "foo", :action => "bar" # and /foo/baz to controller => "foo", :action => "caz" r.match "/foo", :controller => "foo" do |f| f.match("/bar").to(:action => "bar") f.match("/baz").to(:action => "caz") end r.match "/foo", :controller => "foo" do |f| f.match("/bar", :action => "bar") f.match("/baz", :action => "caz") end # => doesn't register any routes at all # match also takes regular expressions r.match(%r[/account/([a-z]{4,6})]).to(:controller => "account", :action => "show", :id => "[1]")
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, set :path if you want to override what appears on the url |
&block: | A new Behavior instance is yielded in the block for nested resources. |
r<Behavior>: | The namespace behavior object. |
r.namespace :admin do |admin| admin.resources :accounts admin.resource :email end # /super_admin/accounts r.namespace(:admin, :path=>"super_admin") do |admin| admin.resources :accounts end
Behavior#resource is a route helper for defining a singular RESTful resource. It yields to a block for child routes.
name<String, Symbol>: | The name of the resource. |
options<Hash>: | Overides and parameters to be associated with the route. |
:namespace<~to_s>: The namespace for this route. :name_prefix<~to_s>:
A prefix for the named routes. If a namespace is passed and there isn't a name prefix, the namespace will become the prefix.
:controller<~to_s>: The controller for this route
next_level<Behavior>: | The child behavior. |
Array: | Routes which define a RESTful single resource. |
r.resources :account # will result in the typical RESTful CRUD # GET /account/new :action => "new" # POST /account/?(\.:format)?, :action => "create" # GET /account/(\.:format)? :action => "show" # GET /account/[;/]edit :action => "edit" # PUT /account/(\.:format)? :action => "update" # GET /account/[;/]delete :action => "delete" # DELETE /account/(\.:format)? :action => "destroy"
You can optionally pass :namespace and :controller to refine the routing or pass a block to nest resources.
r.resource :account, :namespace => "admin" do |account| account.resources :preferences, :controller => "settings" end
@public
Behavior#resources is a route helper for defining a collection of RESTful resources. It yields to a block for child routes.
name<String, Symbol>: | The name of the resources |
options<Hash>: | Ovverides and parameters to be associated with the route |
:namespace<~to_s>: The namespace for this route. :name_prefix<~to_s>:
A prefix for the named routes. If a namespace is passed and there isn't a name prefix, the namespace will become the prefix.
:controller<~to_s>: The controller for this route :collection<~to_s>: Special settings for the collections routes :member<Hash>:
Special settings and resources related to a specific member of this resource.
:keys<Array>:
A list of the keys to be used instead of :id with the resource in the order of the url.
next_level<Behavior>: | The child behavior. |
Array: | Routes which will define the specified RESTful collection of resources |
r.resources :posts # will result in the typical RESTful CRUD # GET /posts/?(\.:format)? :action => "index" # GET /posts/index(\.:format)? :action => "index" # GET /posts/new :action => "new" # POST /posts/?(\.:format)?, :action => "create" # GET /posts/:id(\.:format)? :action => "show" # GET /posts/:id[;/]edit :action => "edit" # PUT /posts/:id(\.:format)? :action => "update" # GET /posts/:id[;/]delete :action => "delete" # DELETE /posts/:id(\.:format)? :action => "destroy" # Nesting resources r.resources :posts do |posts| posts.resources :comments end
Creates a Route from one or more Behavior objects, unless a block is passed in.
params<Hash>: | The parameters the route maps to. |
&block: | Optional block. A new Behavior object is yielded and further to operations may be called in the block. |
new_behavior<Behavior>: | The child behavior. |
Route: | It registers a new route and returns it. |
r.match('/:controller/:id).to(:action => 'show') r.to :controller => 'simple' do |s| s.match('/test').to(:action => 'index') s.match('/other').to(:action => 'other') end
conditions<Hash>: | The conditions to compile. Defaults to merged_conditions. |
Hash: | The compiled conditions, with each value as a Regexp object. |
params<Hash>: | The params to compile. Defaults to merged_params. |
placeholders<Hash>: | The route placeholders for this behavior. Defaults to merged_placeholders. |
String: | The params hash in an eval‘able form. |
compiled_params({ :controller => "admin/:controller" }) # => { :controller => "'admin/' + matches[:path][1]" }
parent<Merb::Router::Behavior>: | The parent behavior for the generated resource behaviors. |
Array: | Behaviors for a singular RESTful resource. |