Add a new asset path in Rails 3.1

Ruby on-RailsPathRuby on-Rails-3.1Asset Pipeline

Ruby on-Rails Problem Overview


Does anyone know how to add another folder to the asset pipeline in Rails 3.1?

I'd like to serve app/assets/fonts the same way app/assets/images is served.


Update: 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:

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.

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.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Andrew, app/assets/fonts is 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.

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.

[1] Assuming you have a font at app/assets/fonts/Ubuntu/Ubuntu-R-webfont.eot

Solution 2 - Ruby on-Rails

Andrew, Jason, agreed. FWIW I put this in my config/application.rb next to

  # Enable the asset pipeline
config.assets.enabled = true
config.assets.paths << "#{Rails.root}/app/assets/fonts"

Solution 3 - Ruby on-Rails

By creating app/assets/images and app/assets/fonts they will be automatically added to the assets path.

Open up rails console after creating them and check with:

y Rails.application.config.assets.paths 

(y is a shortcut for the yaml method)

Solution 4 - Ruby on-Rails

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

Solution 5 - Ruby on-Rails

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.

Solution 6 - Ruby on-Rails

Create assets/fonts folder and add some font on it and use theme on your css file as follow

@font-face {
  font-family: Sawasdee;
  src: url(Sawasdee.ttf);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrewView Question on Stackoverflow
Solution 1 - Ruby on-RailsJason L PerryView Answer on Stackoverflow
Solution 2 - Ruby on-RailsPeter EhrlichView Answer on Stackoverflow
Solution 3 - Ruby on-RailsocodoView Answer on Stackoverflow
Solution 4 - Ruby on-RailsmontrealmikeView Answer on Stackoverflow
Solution 5 - Ruby on-Railsmv_houseView Answer on Stackoverflow
Solution 6 - Ruby on-RailsS.M.MousaviView Answer on Stackoverflow