如何在资产管道中包括ActionView帮助器? [英] How to include ActionView helpers in the assets pipeline?

查看:99
本文介绍了如何在资产管道中包括ActionView帮助器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在资产管道执行上下文中包括可使用的Rails视图助手?

How to include Rails view helpers to be accesible by assets pipeline execution context?

一个示例用例是使用form_tag辅助方法为表单生成标记,并将其提供给Javascript模板(如把手,jst等)使用.

An example use case would be to generate the markup for a form, using form_tag helper method, and make it available to a Javascript template (like handlebars, jst, etc.).

我使用handlebar_assets gem,但这也应适用于任何erbhaml模板.

I use handlebar_assets gem, but this should apply to any erb or haml template too.

推荐答案

创建一个初始化程序,并在资产的上下文中包含帮助程序,如下所示:

Create a inititializer and include the helpers in the context of the assets like this:

Rails.application.assets.context_class.class_eval do
  include ActionView::Helpers
  include MyAppHelper
  include Rails.application.routes.url_helpers
end

来自此链轮问题

这篇关于如何在资产管道中包括ActionView帮助器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆