Using a Rails helper method within a javascript asset

前端 未结 5 2190
长发绾君心
长发绾君心 2020-11-28 03:13

Is there any way to use a Rails helper method, more specifically, a path helper method within a javascript asset file. This file foo.js.coffee.erb



        
5条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 03:31

    Indeed I agree with @eirc answer (taking into account Sprockets documentation). But I would consider including helper modules at rails boot time. As per

    # 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 because of: https://github.com/rails/sprockets-rails/blob/master/lib/sprockets/railtie.rb#L23

提交回复
热议问题