Skip to content

plastictrophy/js-named-routes

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Named Routes in Javascript

This "creatively" named plugin installs a controller and view which creates handy javascript methods for generating named routes in javascript. For example, if you had the following RESTful route defined in routes.rb:

map.resources :posts

You'd get these (and other) familiar-looking javascript URL helpers:

posts_url()         => "http://localhost:3000/posts"
post_url({id: 3})   => "http://localhost:3000/posts/3"
post_path({id: 3})  => "/posts/3"

post_path({
  id: 3, 
  format: 'xml'
}) => "/posts/3.xml"

post_path({
  id: 3,
  extra: 'string'
}) => '/posts/3?extra=string'

The hostname is also made available:

js_named_routes_server_host => "http://localhost:3000"

If you prefer to have routes added to their own namespace instead of the global javascript namespace, specify the namespace of your choosing as follows in an initializer:

JsNamedRoutes::Config.scope = "Routing"

Use the scoped style helpers like this:

Routing.posts_url()         => "http://localhost:3000/posts"
Routing.host                => "http://localhost:3000"

The javascript generated by this plugin does not depend on any third-party javascript library.

INSTALL

$ cd RAILS_ROOT
$ script/plugin install git://github.com/gohanlonllc/js_named_routes.git

This plugin requires Rails 2.3.2 or newer because it's (now) implemented as an engine-style plugin.

USAGE

This plugin automatically injects a route for the included JS named routes controller.

Install, and then visit the following URL to confirm the plugin is working properly:

http://localhost:3000/javascripts/named_routes.js

Include the js_named_routes javascript URL in your HTML head tag (probably application.html.erb or other layout file):

<%= javascript_include_tag named_routes_path %>

This plugin uses page caching to cache the generated javascript routes, but lacks any intelligence for sweeping the cached file if you've made changes to your routes. That shouldn't be a problem if you are using Capistrano for production deployments because the cache is normally stored in RAILS_ROOT/public which isn't kept across deployments. Caching is normally disabled in development mode, but if you turn on caching you will need to manually clear the cached file:

$ cd RAILS_ROOT
$ rm public/javascripts/named_routes.js

CREDITS

Originally authored by Joshua Siereles with some code and the idea coming from Using Rails Routes from Client-Side JavaScript by Galen O'Hanlon.

Enhancements and Rails 2.3.2 compatibility added by Gohanlon LLC.

Original non-configurable scoping implementation utilizing Routing object wrapper and Rails 2.0 compatibility added and cleaned up by Ripta Pasay.

About

Rails named routes in javascript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 100.0%