Ruby Rails预编译资产与使用资产管道

时间:2016-03-14 17:10:32

标签: javascript ruby-on-rails plugins asset-pipeline precompile

我想知道何时使用Rails.application.config.assets.precompile来包含Javascript插件,以及何时使用资产管道。据我所知,资产管道缩小了其中列出的所有文件,并使整个应用程序可以使用这个缩小的文件。但是,您可以在 config / initializers / assets.rb 中使用Rails.application.config.assets.precompile,以允许插件仅在特定页面上使用。这种理解是否正确?

例如,如果我的 config / initializers / assets.rb 文件中有以下行:

Rails.application.config.assets.precompile += %w( plugins/footable/angular-footable.js )
Rails.application.config.assets.precompile += %w( plugins/footable/footable.all.min.js )
Rails.application.config.assets.precompile += %w( plugins/footable/footable.core.css )

然后我可以在 app / views / users / index.html.erb 文件中使用Foo Table插件:

<%= javascript_include_tag 'plugins/footable/angular-footable' %>
<%= javascript_include_tag 'plugins/footable/footable.all.min' %>
<%= stylesheet_link_tag 'plugins/footable/footable.core' %>

这是正确的用法吗?

我问的原因是因为我只想在 app / views / users / index.html.erb 页面上使用Foo Table插件(以及其他)。我考虑过将这个插件包含在资产管道中,但是我并不想使用只需要一页的Javascript代码来破坏我的应用程序的其余部分。

1 个答案:

答案 0 :(得分:3)

我在这个网站上找到了自己问题的答案:https://launchschool.com/blog/rails-asset-pipeline-best-practices。基本上,答案是肯定的,我的理解是正确的。

以下是文章的相关部分: enter image description here

enter image description here

相关问题