0.0
No commit activity in last 3 years
No release in over 3 years
Generates a JavaScript file containing specific url_helpers defined in config/routes.rb
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.12
~> 10.0

Runtime

>= 4.2.0
 Project Readme

Js::Routes::Rails

Generates a JavaScript file containing a predefined set of Rails URL helpers on the client.

Installation

Add this line to your application's Gemfile:

gem 'js-routes-rails'

And then execute:

$ bundle

Set up

Append export: true to any route defined in you config/routes.rb which declares that resource able to be exported to the client.

# config/routes.rb
resources :articles, export: true

resources :comments

resources :publishers, export: true do
  resources :magazines do
    resources :photos
  end
end

match "/about", to: "static#index", via: :get, export: true

And then execute:

$ bundle exec rake js_routes_rails:export

This will generate a JavaScript file in app/assets/javascripts/js-routes-rails.js, which you can consume using the Asset Pipeline, for example:

<%= javascript_include_tag 'js-routes-rails' %>

or in application.js

//= 'js-routes-rails'

You can also configure the location and format of the generated JavaScript file in your application.rb

Js::Routes::Rails.configure do |c|
  c.output = '/some/other/place.js'
  c.template = :commonjs
end

Usage

Once the generated file is in your project you're able to access the JsRoutesRails object which contains all the exported Rails URL helpers on the client.

JsRoutesRails.new_articles_path() // '/articles/new'
JsRoutesRails.edit_articles_path({ id: 23 }) // '/articles/23/edit'
JsRoutesRails.edit_magazines_path({ publishers_id: 5, id: 9 }) // '/publishers/5/magazines/9/edit'

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/damian/js-routes-rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.