从 Rails 控制器访问资产路径 [英] Access Asset Path from Rails Controller
问题描述
我正在共享一个配置 yml 文件客户端,我也需要在服务器端加载它,我将它放在 app/assets/javascripts/configuration.yml 中
I'm sharing a configuration yml file client side, that I need to also load on the server side, I've placed it inside app/assets/javascripts/configuration.yml
我可以在视图中使用 #{asset_path 'configuration.yml'} 来获取路径,但我不能在控制器中使用.我可以使用#{Rails.root}/app/assets/javascripts/configuration.yml"直接访问,但是在部署文件名时会附加摘要字符串.
I can use #{asset_path 'configuration.yml'} inside a view to get the path, but I can't inside a controller. I could access directly using "#{Rails.root}/app/assets/javascripts/configuration.yml" but when deploying the filename gets the digest string appended.
如何从控制器获取相同的路径?
How can I get the same path from a controller?
推荐答案
ActionController::Base.helpers.asset_path("configuration.yml")
将 configuration.yml 放在不同的文件夹中以将 javascript 与非 javascript 文件分开也可能会很好.
Might also be good to put configuration.yml in a different folder to separate javascript from non-javascript files.
这篇关于从 Rails 控制器访问资产路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!