仅在存在资产时包括资产 [英] Include Assets Only If They Exist
本文介绍了仅在存在资产时包括资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在当前的Rails应用中,我们遵循某些模式来包含资产,例如脚本和样式表.
In our current rails app, we are following certain patterns for including assets such as scripts and stylesheets.
例如,一种这样的模式是(布局内的代码):
For instance, one such pattern is (code inside the layout):
= stylesheet_link_tag controller.controller_name
这里的问题是不是 all 的所有控制器都将具有关联的样式表.检查资产是否存在的最佳方法是什么?具体来说,由于缓存破坏资产名称,我知道这里有些技巧.
The problem here is that not all of the controllers are going to have associated stylesheets. What is the best way to check if an asset exists? Specifically, I know there is some trickery here due to the cache busting asset names.
推荐答案
最后弄清楚了这一点.可以通过以下方式检查资产的存在:
Finally figured this one out. Asset existence can be checked as follows:
YourApp::Application.assets.find_asset("#{asset}.css").nil?
答案将是:
= stylesheet_link_tag controller.controller_name if YourApp::Application.assets.find_asset("#{controller.controller_name}.css")
这篇关于仅在存在资产时包括资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文