在 javascript 资产中使用 Rails 辅助方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7451517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:20:28  来源:igfitidea点击:

Using a Rails helper method within a javascript asset

javascriptruby-on-railsruby-on-rails-3asset-pipelineerubis

提问by axsuul

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

有什么方法可以使用 Rails 辅助方法,更具体地说,是 javascript 资产文件中的路径辅助方法。这个文件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")

回答by eirc

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.rband 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.

要使用 url 助手,您必须包含特定应用程序的助手。

They are available at Rails.application.routes.url_helpersso:

它们可在以下位置获得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.

编辑:修复了移动链轮存储库的链接,但不确定这么多年后这仍然有意义。

回答by user1158559

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...). There is a ruby gem which reimplements all of your Rails UrlHelpers in JavaScript: https://github.com/railsware/js-routes

您使用 UrlHelpers 的方法将有效,直到您需要找到post_path(...um...we don't know what post id we'll need at compile time...). 有一个 ruby​​ gem,它在 JavaScript 中重新实现了所有 Rails UrlHelpers:https: //github.com/railsware/js-routes

回答by topek

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

  • 您将阻止缓存 javascript 文件,因为它包含一个动态值

  • 你会在你的 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. Just like 'Unobtrusive Scripting Adapters' for Rails do. E.g.: https://github.com/rails/jquery-ujs/blob/master/src/rails.js#L157-173

如果可能,我会建议您通过在视图中提供目标路径并在特定事件发生时从 DOM-Element 检索值来抽象您的问题。就像 Rails 的“Unobtrusive Scripting Adapters”一样。例如:https: //github.com/rails/jquery-ujs/blob/master/src/rails.js#L157-173

回答by Andrea Amantini

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

事实上,我同意 @eirc 的回答(考虑到链轮文档)。但我会考虑在 Rails 启动时包含辅助模块。按照

# 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

回答by Matthew

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 .erbassets

在您的.erb资产中