简体   繁体   English

在 Rails 3.1 中添加新的资产路径

[英]Add a new asset path in Rails 3.1

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/fonts the same way app/assets/images is served.我想以与提供 app/ app/assets/images相同的方式提供app/assets/fonts


Update: 5-7-2013更新:2013 年 5 月 7 日

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/assets you 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/assetslib/assetsvendor/assets之外的路径,并且请记住,虽然 Sprockets 会在应用程序加载时存在的文件夹中拾取新文件,但根据我的经验,它不会无需重新启动即可在资产路径中拾取新文件夹。

Andrew, app/assets/fonts is actually already in your asset load path, along with images .安德鲁, app/assets/fonts实际上已经在您的资产加载路径中,还有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.所以你可以用同样的方式指向资产: <%= 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/fonts and app/assets/images .我仍然不知道如果app/assets/fontsapp/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有一个字体

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 creating app/assets/images and app/assets/fonts they will be automatically added to the assets path.通过创建app/assets/imagesapp/assets/fonts ,它们将自动添加到资产路径中。

Open up rails console after creating them and check with:创建它们后打开rails控制台并检查:

y Rails.application.config.assets.paths 

( y is a shortcut for the yaml method) yyaml方法的快捷方式)

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 ?#iefix outside the font file name例如,在这种情况下,将?#iefix留在字体文件名之外

I can confirm it works without adding the new paths to the config in Rails 3.1.0.rc4 (and presumedly higher).我可以确认它可以在不向 Rails 3.1.0.rc4 中的配置添加新路径的情况下工作(可能更高)。 I bounced my server, you might do the same.我反弹了我的服务器,你也可以这样做。

Create assets/fonts folder 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM