Sneaky Abstractions

Subscribe to my Feed, follow me on , recommend me on Working With Rails or see my code on GitHub

JavaScript Routes

JavascriptRoutes converts your Rails routes into JavaScript. You can then access (generate) them in the browser (or any other JS environment). It supports both normal and named routes, and creates helper functions for the latter.

Installing

Download from Github and put in vendor/plugins.

Using

The plugin will generate public/javascripts/routes.js when the application starts. The file will vary in size depending on how many routes you have in routes.rb. The “required” parts are around 3kb. To use it, simply include it like any other JavaScript file in your layout:

<script type="text/javascript" src="/javascripts/routes.js"></script>

The file will define two objects that will be available to the browser. A Route encapsulates a single route, while Routes contain all routes (it’s an array). Routes.named contains all the named routes. To generate a route, call Routes.generate:

Routes.generate({controller:'foo', action:'foo'})
Routes.generate({controller:'bar', action:'baz', baz:'quux'}, {host:'example.com'})

If no routes are found to match the parameters, it will return false. The generate function takes an object (hash) with name/value mappings and an optional options object. The available options are:

onlyPath
Don’t include the domain
includeSlash
Include the last slash in the path
escape
Set to false to prevent the escaping of special characters
protocol
Protocol other than ‘http://’ to use
host
Hostname
port
Port number
noDefaults
Don’t use Routes.defaultParams when generating URL

If you want to use a named route, it will have defined a convenience method on the Routes object:

Routes.home()
Routes.articles()
Routes.article(1)
Routes.article({id:1, foo:'bar'})

These functions can, like their Rails equivalents, take either a hash with name/value mappings, or all required arguments in the correct order. They also take an optional second (or last) argument options.

Default parameters and options

The Routes object has two properties, defaultParams and defaultOptions, that can be filled with default values for the params and options arguments passed when generating URLs. You could, for example, set a default controller name:

Routes.defaultParams.controller = 'articles';
Routes.generate({action:'show', id:1});
// => "/articles/1" (if RESTful)
//You can set any parameter you want
Routes.defaultParams.foo = 'bar';
Routes.generate({controller:'users', :action:'index'});
// => "/users?foo=bar"
Routes.generate({controller:'users', :action:'index', foo:'rab'});
// => "/users?foo=rab"
delete Routes.defaultParams.foo;
Routes.defaultParams.id = 1;
Routes.user();
//=> "/users/1"
Routes.user(2);
//=> "/users/2"

Routes.defaultParams.action is already set to “index”, which is the default action for any controller. You can delete this if that’s not what you want of course. Parameters passed to the generate method or default parameters from a named route will overwrite parameters from defaultParams.

Routes.defaultOptions contains the default values passed as the options (second) argument when generating URLs. Some values, like host and port will already have been set to the current host name and port number.

Routes.defaultOptions.host = 'example.com';
Routes.generate({controller:'users'});
//=> "http://example.com/users"
Routes.defaultOptions.onlyPath = true;
Routes.generate({controller:'users'});
//=> "/users"
Routes.defaultParams.foo = 'bar';
Routes.generate({controller:'users'});
//=> "/users?foo=bar"
Routes.defaultParams.noDefaults = true;
Routes.generate({controller:'users'});
//=> "/users"

As with defaultParams, values in defaultOptions will be overwritten by options passed in at generation time.

Comments

0 comments

They tried the freeway, they tried the highway, they tried any which way, and they were taken down!