简体   繁体   English

在javascript资产中使用Rails帮助器方法

[英]Using a Rails helper method within a javascript asset

Is there any way to use a Rails helper method, more specifically, a path helper method within a javascript asset file. 有没有办法使用Rails助手方法,更具体地说,是javascript资产文件中的路径助手方法。 This file foo.js.coffee.erb 这个文件是foo.js.coffee.erb

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

I would love it if I could get from erubis 如果我能从erubis那里得到,我会喜欢它

$('#bar').val("path/to/create")

You can include any helper/module/class in an erb template with: 您可以在erb模板中包含任何帮助器/模块/类:

<% 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 请参阅: https//github.com/rails/sprockets/blob/master/lib/sprockets/environment.rbhttps://github.com/rails/sprockets/blob/master/lib/sprockets/context.rb

To use the url helpers you have to include your specific applications' helpers. 要使用网址助手,您必须包含特定应用程序的帮助程序。

They are available at Rails.application.routes.url_helpers so: 它们可以在Rails.application.routes.url_helpers中找到:

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

EDIT: Fixed links to moved sprockets repo but not really sure this still makes sense so many years later. 编辑:固定链接到移动的链轮回购但不确定这仍然有多年后的意义。

This will also do the trick in an initializer: 这也可以在初始化器中完成:

Sprockets::Context.send :include, MyHelper

In development mode the helper will not be reloaded 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...) . 您使用UrlHelpers的方法将一直有效,直到您需要找到post_path(...um...we don't know what post id we'll need at compile time...) There is a ruby gem which reimplements all of your Rails UrlHelpers in JavaScript: https://github.com/railsware/js-routes 有一个ruby gem在JavaScript中重新实现了所有的Rails UrlHelpers: https//github.com/railsware/js-routes

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 您可以阻止缓存javascript文件,因为它包含动态值

  • You would get a coupling between your javascript/coffeescript and rails 你会得到你的javascript / coffeescript和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. 如果可能,我会建议您通过在视图中提供目标路径来抽象问题,并在特定事件发生时从DOM元素中检索值。 Just like 'Unobtrusive Scripting Adapters' for Rails do. 就像Rails的'Unobtrusive Scripting Adapters'一样。 Eg: https://github.com/rails/jquery-ujs/blob/master/src/rails.js#L157-173 例如: https//github.com/rails/jquery-ujs/blob/master/src/rails.js#L157-173

Indeed I agree with @eirc answer (taking into account Sprockets documentation). 确实,我同意@eirc的答案(考虑到Sprockets文档)。 But I would consider including helper modules at rails boot time. 但我会考虑在rails启动时包含辅助模块。 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 这是因为: https//github.com/rails/sprockets-rails/blob/master/lib/sprockets/railtie.rb#L23

Actually, not sure if this helps but there is a way to define your own helpers for use during rake precompile 实际上,不确定这是否有帮助,但有一种方法可以定义自己的帮助程序,以便在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 在你的.erb资产中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM