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

查看:77
本文介绍了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文件加载为.".指的是本地文件夹(在这种情况下,是引擎javascript的文件夹).

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天全站免登陆