Using a Rails helper method in a javascript resource

Is there any way to use the helper Rails method, and more specifically, the path helper method in the javascript asset file. This file is foo.js.coffee.erb

 $('#bar').val("<%= create_post_path %>") 

I would love it if I could get from erubis

 $('#bar').val("path/to/create") 
+63
javascript ruby-on-rails ruby-on-rails-3 asset-pipeline erubis
Sep 17 2018-11-11T00:
source share
5 answers

You can include any helper / module / class in the erb template with:

 <% environment.context_class.instance_eval { include MyHelper } %> 

See: https://github.com/rails/sprockets/blob/master/lib/sprockets/environment.rb and https://github.com/rails/sprockets/blob/master/lib/sprockets/context.rb

To use URL assistants, you must enable assistants for your specific applications.

They are available at Rails.application.routes.url_helpers , therefore:

 <% environment.context_class.instance_eval { include Rails.application.routes.url_helpers } %> 

EDIT: Fixed references to moving sprockets repo, but was not really sure if it still made sense so many years later.

+67
Nov 15 2018-11-11T00:
source share

This will also do the trick in the initializer:

 Sprockets::Context.send :include, MyHelper 

In development mode, the helper will not restart on every request.

Your approach with UrlHelpers will work until you need to find post_path(...um...we don't know what post id we'll need at compile time...) . There is a ruby ​​pearl that repeats all your Rails UrlHelpers in JavaScript: https://github.com/railsware/js-routes

+25
Dec 12 '12 at 14:35
source share

I'm not quite sure why you want to do this.

  • You have denied caching of javascript file as it contains dynamic value

  • You will get the link between your javascript / coffeescript and rails

If possible, I would advise you to distract your problem by providing the target path in the view and get the value from the DOM element when a specific event occurs. Just like "Unobtrusive Script Adapters" for Rails. For example: https://github.com/rails/jquery-ujs/blob/master/src/rails.js#L157-173

+18
Sep 18 '11 at 20:24
source share

Indeed, I agree with @eirc's answer (subject to Sprockets documentation). But I would think of enabling helper modules during rails loading. According to

 # config/initializers/sprockets.rb Rails.application.assets.context_class.instance_eval do include ActionView::Helpers include Rails.application.routes.url_helpers include MyModule end 

this is due to: https://github.com/rails/sprockets-rails/blob/master/lib/sprockets/railtie.rb#L23

+8
Dec 03 '14 at 10:09
source share

Actually, not sure if this helps, but there is a way to define your own helpers for use during rake precompile

Create a new initializer and put it in it:

 module Sprockets module Helpers module RailsHelper def my_helper_method ... end end end end 

And then you can:

 <%= my_helper_method %> 

in your .erb assets

+5
Oct 08 2018-11-11T00:
source share



All Articles