使用 Rails 3.1 的引擎资产 [英] Engine's assets with Rails 3.1

查看:26
本文介绍了使用 Rails 3.1 的引擎资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应该如何在 Rails 3.1 中的引擎中提供资产?它们应位于何处,是否可以自动包含在内?

How should one provide assets in an engine in Rails 3.1? Where should they be located and can they be included automatically?

(最初由 Tomas Celizna 询问)

推荐答案

自动加载所有引擎的资产文件夹的路径.

The paths to the all the engines' assets folders are automatically loaded.

默认情况下不会加载资源本身.这是可以理解的,因为加载是通过 require_tree . 完成的,它从当前文件夹(即主应用程序资产的文件夹)加载所有 css/js,但没有说明引擎资产的任何内容.

The assets themselves are not loaded by default. This is understandable as the loading is done with require_tree ., which loads all css/js from the current folder (i.e. the main application assets' folder) but doesn't say anything about the engines assets.

简单的解决方案是要求用户在 application.js/css 或其他任何需要的地方要求 js/css.由于路径加载正确,用户只需要指定您的资产名称(我建议使用您的引擎名称).示例:

The easy solution is to ask the user to require the js/css in application.js/css or wherever else it is needed. As the paths are loaded correctly, the user only need to specify the name of your asset (I'd recommend using the name of your engine). Example:

附加到main_app/app/assets/javascripts/application.js:

//= require your_engine_name

如果您已将 js 拆分为不同的文件,则您的文件 your_engine_name/app/assets/javascripts/your_engine_name.js 可能包含以下内容:

If you have split your js in different files, your file your_engine_name/app/assets/javascripts/your_engine_name.js could have the following:

//= require_tree .

这将加载 your_engine_name/app/assets/javascripts/ 中的所有 js 文件,作为."指的是本地文件夹(在本例中是您引擎的 javascripts 文件夹).

This will load all js files in your_engine_name/app/assets/javascripts/, as the "." refers to the local folder (in this case the folder of your engine's javascripts).

请注意,当设置了 config.use_sprockets 时,ActionView::Helpers::AssetTagHelper.register_javascript_expansion 似乎没有任何效果.我希望他们至少会在这种情况下发出警告.

Note that ActionView::Helpers::AssetTagHelper.register_javascript_expansion appears not to have any effect when config.use_sprockets is set. I hope they'll at least put a warning in that case.

如果您有安装引擎的 rake 任务,那么您可以将附加到 application.js.

If you have a rake task to install your engine, then you could do the append to application.js.

用户包含它的另一种方法是在 erb 布局中插入 <%= javascript_include_tag "your_engine_name" %>.

Another way for the user to include it is to insert <%= javascript_include_tag "your_engine_name" %> in the erb layout.

我认为没有办法让它自动插入

I don't think there is a way to have it inserted automatically

这篇关于使用 Rails 3.1 的引擎资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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