简体   繁体   English

引擎在 Rails 3.1 中的资产

[英]Engine's assets with Rails 3.1

How should one provide assets in an engine in Rails 3.1?应该如何在 Rails 3.1 的引擎中提供资产? Where should they be located and can they be included automatically?它们应该位于何处,是否可以自动包含在内?

( originally asked by Tomas Celizna ) 最初由 Tomas Celizna 提出

The paths to the all the engines' assets folders are automatically loaded.自动加载所有引擎资产文件夹的路径。

The assets themselves are not loaded by default.默认情况下不加载资产本身。 This is understandable as the loading is done with require_tree.这是可以理解的,因为加载是使用require_tree. , which loads all css/js from the current folder (ie the main application assets' folder) but doesn't say anything about the engines assets. ,它从当前文件夹(即主应用程序资产的文件夹)加载所有 css/js,但没有说明引擎资产。

The easy solution is to ask the user to require the js/css in application.js/css or wherever else it is needed.简单的解决方案是要求用户在 application.js/css 或其他任何需要的地方要求 js/css。 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:例子:

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

//= require your_engine_name

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:如果您将 js 拆分为不同的文件,您的文件your_engine_name/app/assets/javascripts/your_engine_name.js可能具有以下内容:

//= require_tree .

This will load all js files in your_engine_name/app/assets/javascripts/ , as the "."这将加载your_engine_name/app/assets/javascripts/中的所有 js 文件,作为“。” refers to the local folder (in this case the folder of your engine's javascripts).指的是本地文件夹(在这种情况下是您引擎的 javascripts 的文件夹)。

Note that ActionView::Helpers::AssetTagHelper.register_javascript_expansion appears not to have any effect when config.use_sprockets is set.请注意,当设置config.use_sprockets时, ActionView::Helpers::AssetTagHelper.register_javascript_expansion似乎没有任何效果。 I hope they'll at least put a warning in that case.我希望他们至少会在这种情况下发出警告。

If you have a rake task to install your engine, then you could do the append to application.js.如果您有安装引擎的 rake 任务,那么您可以对 application.js 执行 append。

Another way for the user to include it is to insert <%= javascript_include_tag "your_engine_name" %> in the erb layout.用户包含它的另一种方法是在 erb 布局中插入<%= javascript_include_tag "your_engine_name" %>

I don't think there is a way to have it inserted automatically我认为没有办法让它自动插入

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

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