Using a Rails helper method within a javascript asset

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

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

    • You would prevent the javascript file from being cached, as it contains a dynamic value

    • You would get a coupling between your javascript/coffeescript and rails

    If possible I would advice you to abstract your problem away by providing your target path in the view and retrieve the value from the DOM-Element when the specific event occurs. Just like 'Unobtrusive Scripting Adapters' for Rails do. E.g.: https://github.com/rails/jquery-ujs/blob/master/src/rails.js#L157-173

提交回复
热议问题