Using a Rails helper method within a javascript asset

前端 未结 5 2203
长发绾君心
长发绾君心 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:28

    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 this 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

提交回复
热议问题