Rails-在application.rb中添加了4个自定义资产路径,无需重新启动 [英] Rails - 4 Custom Asset Path added in application.rb without restart
问题描述
路轨4
对于下面提到的字体,
@font-face {
font-family: "Verdana";src: url(/assets/Verdana.ttf) format("truetype");
}
文件位置为 app/assets/fonts (由rails本身加载)
Location of file is app/assets/fonts (loaded by rails itself)
现在,如果我在 app/assets/fonts/client_4 文件夹中拥有此文件,
Now if I have this file in app/assets/fonts/client_4 folder,
我必须在application.rb中添加以下内容
I had to add following in application.rb
config.assets.paths << Rails.root.join('app', 'assets', 'fonts', 'client_4')
或
Dir.glob("#{Rails.root}/app/assets/fonts/**/").each do |path|
config.assets.paths << path
end
它可以与提供为url(/assets/Verdana.ttf)的字体面孔url一起使用,但是需要重新启动服务器才能加载application.rb
It worked with font-face url provided as url(/assets/Verdana.ttf), but need to restart server to load application.rb
查询
我的字体文件是由用户上传并存储在app/assets/fonts目录中新创建的client_x文件夹中的.
Case is my font file is uploaded by user and stored in newly created client_x folder present in app/assets/fonts directory.
在这种情况下,我想为该client_x文件夹添加asset_path而不重启服务器.请提出建议.
I want to add asset_path in this case for that client_x folder without restarting server. Please suggest.
更新
无论何时创建目录,我都在控制器中添加了以下内容,
I added following in controller whenever directory is created,
MyApplication::Application.config.assets.paths << Rails.root.join('app', 'assets', 'fonts', "client_#{@client.id}/").to_s
然后我通过erb标签在页面中进行了检查,
And I checked following in page by erb tag,
= puts MyApplication::Application.config.assets.paths.inspect
我得到以下输出,
[
[ 0] "/home/..../app/assets/fonts",
[ 1] "/home/..../app/assets/images",
[ 2] "/home/..../app/assets/javascripts",
[ 3] "/home/..../app/assets/stylesheets",
[ 4] "/home/..../vendor/assets/javascripts",
.
.
[20] "/home/..../app/assets/fonts/client_164/",
[21] "/home/..../app/assets/fonts/client_163/"
]
-
第20条路径由application.rb&服务器启动时加载,我确实通过asset_url在font-face中获得了字体文件
20th path is added by application.rb & loaded when server started, I do get font file by asset_url in font-face
第21条路径由控制器和&我没有通过asset_url的字体获得字体文件.
21st path is added by controller and & I do not get font file by asset_url in font-face.
如果检查可以看到资产中的两个路径,我没有得到,仍然找不到控制器在client_163内添加的字体文件的资产url.我想要它而无需重新启动服务器.请建议eagerload或autoload是否可以提供帮助.
I am not getting if I inspect I can see both path in assets , still I do not get asset url for font files inside client_163 added by controller. I want it without restarting server. Please suggest if eagerload or autoload can help.
推荐答案
我无法准确获得想要的东西,但找到了替代方法.
I could not get exact what I wanted but I found alternative to work with.
我没有在app/assets/fonts/中添加子目录的资产路径.
I did not add asset path for sub-directories inside app/assets/fonts/.
我使用了完整的网址,例如#{APP_URL}/assets/client _#{client_id}/#{self [file]}" 会产生类似" http://localhost:3000/assets/client_163/GreatVibes-Regular的网址.otf '
I worked with complete url like "#{APP_URL}/assets/client_#{client_id}/#{self[file]}" which produce url like 'http://localhost:3000/assets/client_163/GreatVibes-Regular.otf'
此处用于开发的APP_URL是在development.rb中设置的" http://localhost:3000 ". 类似地添加了分期&生产.
Here APP_URL for development is, 'http://localhost:3000' set in development.rb. Similarly added for staging & production.
更好地将变量APP_URL保留在application.yml中(实现figaro gem)&以#{ENV [APP_URL]}"访问(注意:在.gitignore中添加application.yml)
Better to keep variable APP_URL in application.yml (implementing figaro gem) & accessing as "#{ENV[APP_URL]}" (note: add application.yml in .gitignore)
等待更好的方法.
更新
在分期&生产我正在为不安全 https url申请字体,所以请帮忙.
In staging & production I am getting font applied for not secure https url so, please help.
这篇关于Rails-在application.rb中添加了4个自定义资产路径,无需重新启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!