Ruby-on-rails 在 Rails 3.1 中添加新的资产路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6510006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add a new asset path in Rails 3.1
提问by Andrew
Does anyone know how to add another folder to the asset pipeline in Rails 3.1?
有谁知道如何在 Rails 3.1 中向资产管道添加另一个文件夹?
I'd like to serve app/assets/fontsthe same way app/assets/imagesis served.
我想以app/assets/fonts同样的方式app/assets/images服务。
Update: 5-7-2013
更新:5-7-2013
Just to add some clarification for future people who find this question to explicitly add an asset path, in your application.rb file:
只是为将来发现此问题的人添加一些说明,以在您的 application.rb 文件中明确添加资产路径:
config.assets.paths << "#{Rails.root}/app/assets/fonts"
However, since the above path is under app/assetsyou don't have to add it explicitly, you just need to restart your rails app so Sprockets can pick it up.
但是,由于上面的路径在下面,app/assets您不必明确添加它,您只需要重新启动 rails 应用程序,以便 Sprockets 可以选择它。
You will have to explicitly add paths that are outside of app/assets,lib/assets, or vendor/assets, and just remember that while Sprockets picks up new files in folders that were present when your application loaded, in my experience it does not pick up new folders in the asset paths without a restart.
您将必须显式添加app/assets, lib/assets, 或之外的路径vendor/assets,并且请记住,虽然 Sprockets 在您的应用程序加载时存在的文件夹中选择新文件,但根据我的经验,它不会在资产路径中选择新文件夹重新启动。
采纳答案by Jason L Perry
Andrew, app/assets/fontsis actually already in your asset load path, along with images. So you can just point to the asset in the same way: <%= asset_path('/Ubuntu/Ubuntu-R-webfont.eot') %>[1] or how ever you are referencing your images.
Andrew,app/assets/fonts实际上已经在您的资产加载路径中,以及images. 所以你可以用同样的方式指向资产:<%= asset_path('/Ubuntu/Ubuntu-R-webfont.eot') %>[1] 或者你如何引用你的图像。
It took me a while to wrap my head around this as well. I still don't know what happens if there's a file with the same name in app/assets/fontsand app/assets/images.
我也花了一段时间来解决这个问题。我仍然不知道是否有在同一个名称的文件会发生什么app/assets/fonts和app/assets/images。
[1] Assuming you have a font at app/assets/fonts/Ubuntu/Ubuntu-R-webfont.eot
[1] 假设你有一个字体 app/assets/fonts/Ubuntu/Ubuntu-R-webfont.eot
回答by Peter Ehrlich
Andrew, Jason, agreed. FWIW I put this in my config/application.rb next to
安德鲁,杰森,同意了。FWIW 我把它放在我的 config/application.rb 旁边
# Enable the asset pipeline
config.assets.enabled = true
config.assets.paths << "#{Rails.root}/app/assets/fonts"
回答by ocodo
By creating app/assets/imagesand app/assets/fontsthey will be automatically added to the assets path.
通过创建app/assets/images,app/assets/fonts它们将被自动添加到资产路径中。
Open up rails console after creating them and check with:
创建它们后打开 rails 控制台并检查:
y Rails.application.config.assets.paths
(yis a shortcut for the yamlmethod)
(y是yaml方法的快捷方式)
回答by montrealmike
It works without adding the path, but be careful that you are using a valid file name for the asset.
它无需添加路径即可工作,但请注意您为资产使用了有效的文件名。
url("#{asset_path 'fontawesome-webfont.eot'}?#iefix") format('embedded-opentype'),
...
url("#{asset_path 'fontawesome-webfont.svg'}#FontAwesome") format('svg');
For example, in this case, leave ?#iefixoutside the font file name
例如,在这种情况下,将?#iefix字体文件名留在外面
回答by ragaskar
I can confirm it works without adding the new paths to the config in Rails 3.1.0.rc4 (and presumedly higher). I bounced my server, you might do the same.
我可以确认它可以在不向 Rails 3.1.0.rc4(并且可能更高)的配置中添加新路径的情况下工作。我退回了我的服务器,你也可以这样做。
回答by S.M.Mousavi
Create assets/fontsfolder and add some font on it and use theme on your css file as follow
创建assets/fonts文件夹并在其上添加一些字体并在您的 css 文件中使用主题,如下所示
@font-face {
font-family: Sawasdee;
src: url(Sawasdee.ttf);
}

