Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Brings Rails named routes to javascript
Ruby JavaScript

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
spec
.document
.gitignore
.rspec
Gemfile
Gemfile.lock
LICENSE.txt
Rakefile
Readme.md
VERSION
js-routes.gemspec

Readme.md

JsRoutes

Generates javascript file that defines all Rails named routes as javascript helpers

Intallation

Your Rails Gemfile:

gem "js-routes"

Your application initializer, like config/initializers/jsroutes.rb:

JsRoutes.generate!(
    :file => "#{Rails.root}app/assets/javascripts/routes.js",
    :default_format => "json",
    :exclude => [/^admin_/, /paypal/, /^api_/]
)

Available options:

  • :file - the file to generate the routes. Default:
    • #{Rails.root}/app/assets/javascripts/routes.js for Rails >= 3.1
    • #{Rails.root}/public/javascripts/routes.js for Rails < 3.1
  • :default_format - Format to append to urls. Default: blank
  • :exclude - Array of regexps to exclude from js routes. Default: []
    • Note that regexp applied to named route not to URL
  • :include - Array of regexps to include in js routes. Default: []
    • Note that regexp applied to named route not to URL
  • :namespace - global object used to access routes. Default: Routes
    • Possible variants: MyProject.routes, MyProjectRoutes

Example options usage:

JsRoutes.generate!(:file => "#{path}/app_routes.js", :namespace => "AppRoutes", :exclude => /^admin_/, :default_format => "json")
JsRoutes.generate!(:file => "#{path}/adm_routes.js", :namespace => "AdmRoutes", :include => /^admin_/, :default_format => "json")

In order to generate routes to string and manipulate them yourself use JsRoutes.generate: Like:

JsRoutes.generate(options)

Usage

This will create a nice javascript file with Routes object that has all the rails routes available:

Routes.users_path() // => "/users"
Routes.user_path(1) // => "/users/1"
Routes.user_path(1, {format: 'json'}) // => "/users/1.json"
Routes.new_user_project_path(1, {format: 'json'}) // => "/users/1/projects/new.json"
Routes.user_project_path(1,2, {q: 'hello', custom: true}) // => "/users/1/projects/2?q=hello&custom=true"

In order to make routes helpers available globally:

$.extend(window, Routes)

Have fun

Something went wrong with that request. Please try again.