Cannot get it to work #98

manuelmeurer opened this Issue Feb 24, 2014 · 4 comments


None yet
2 participants

manuelmeurer commented Feb 24, 2014

I'm having trouble getting this to work.

I have a Rails v4.1.0.rc1 app with an application namespace and an admin namespace, so I want to generate AppRoutes and AdminRoutes.

I use js-routes v0.9.7 and this is my initializer (just the AdminRoutes for now)

# config/initializers/js_routes.rb
require 'js_routes'

JsRoutes.generate! \
  file:      File.join('app', 'assets', 'javascripts', 'admin_routes.js'),
  namespace: 'AdminRoutes',
  include:   /admin/

Should I then use = require js-routes or = require admin_routes?

When I use = require js-routes, AdminRoutes is undefined and Routes contains all routes, not just the admin ones:

When I use = require admin_routes, Routes is undefined and AdminRoutes just contains options but no routes:

Both cases are not what I expect.
The file app/assets/javascripts/admin_routes.js is generated in both cases but apart from the boilerplate code doesn't contain any routes.
Very confusing all that. What am I doing wrong?


le0pard commented Feb 24, 2014

Why do you put generation of js files in initializer? If you need both routes, just add this in your js file with .erb prefix - assets pipeline will do all work. Example:

/* app/assets/javascripts/routes.js.erb */
<%= JsRoutes.generate(namespace: "Routes", exclude: [/admin/]) %>
<%= JsRoutes.generate(namespace: "AdminRoutes", include: /admin/) %>

manuelmeurer commented Feb 24, 2014

That works, thanks! But where would you use JsRoutes.generate! if not in an initializer?


le0pard commented Feb 24, 2014

@manuelmeurer for example, in rake task: if you need deploy routes, but impossible to load env for assets pipeline (like on heroku). Or you just need generate static routes once: again or rake task, or some Ruby script.


manuelmeurer commented Feb 24, 2014

Ok, the README is not very clear on that, I will submit a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment