78

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.

Andrew
  • 42,517
  • 51
  • 181
  • 281
  • `config.assets.paths << "#{Rails.root}/app/assets/active_admin"` is not working for me. I still get an error when trying to view a certain page after deploying to staging that the `.js` file in that folder is not compiled. – sixty4bit Nov 26 '14 at 16:08

6 Answers6

74

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

Jason L Perry
  • 1,225
  • 11
  • 7
  • 21
    For what it's worth, I tried this and it didn't seem to work. Then I guessed at `config.assets.paths << "#{Rails.root}/app/assets/fonts"` and that worked. I think you're right that anything under `/assets` is supposed to be included automatically, but for some reason for me it was giving 404 until I added the line above to my `application.rb` file. I'm going to accept your answer though, because I think it will be correct when the release is finalized, and because with this comment tacked on anyone who reads it can figure it out :) – Andrew Jun 29 '11 at 01:09
  • 39
    I just stumbled upon this too, and it works with the default settings, BUT the directories are scanned during app initialization, so if you add a folder like fonts after your server is running you will get a 404. So always restart when you add a directory in your asset pipeline. – Martin Wawrusch Aug 13 '11 at 08:01
  • 4
    To elaborate on files of the same name: When there are naming conflicts, the first path that appears in the `config.assets.paths` array is the file that is chosen. This can be avoided by using the `asset_path()` helper and specifying the directory. – Joseph Ravenwolfe Nov 14 '11 at 19:16
  • 1
    note that the duplicate name issue should normally not pose a problem. the assets being served will have a different format normally so this problem would not occur. Also the asset pipeline gives assets a seed in the name. this is A to break the local cashe of the user's browser but it is also to make sure all files have a unique name. on pre-compile (that you run in production) a yaml file is created in the app linking the generated files to the original path. This allows for quick linking from in-code naming to compiled asset name. This at the same time avoids the duplicate name issues. – yopefonic Sep 10 '12 at 13:18
  • This was perfectly answered over here: http://stackoverflow.com/a/10907276/1407541 – Kopty Apr 30 '13 at 13:00
43

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"
Peter Ehrlich
  • 6,969
  • 4
  • 49
  • 65
  • 15
    From what I've seen the preferred way of writing this is now `Rails.root.join('app', 'assets', 'fonts')` – Ross Allen May 15 '12 at 16:32
  • 1
    A bit annoying when searching from google for a way to *actually* add a new asset path and the accepted answer doesn't actually accomplish the task :p (The asker may not have actually needed to do that, but this is what comes up in the search results). So... +1 for answering the title's question instead of solving the OP's problem. – nzifnab Feb 23 '13 at 19:02
  • config.assets.paths << "#{Rails.root}/app/assets/fonts" worked for me on Rails 3.2.13 – dc10 Aug 07 '13 at 20:54
  • @dc10 Using `Rails.root.join` should be the way you do it, to be flexible across platforms. – ocodo Nov 03 '13 at 09:22
8

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)

ocodo
  • 29,401
  • 18
  • 105
  • 117
3

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

montrealmike
  • 11,433
  • 10
  • 64
  • 86
  • Thank you! This was the cause of an assets:precompile error I started seeing after adding fonts, which results in a very unhelpful "rake aborted! undefined method `match' for nil:NilClass" – Geoff Mar 27 '13 at 01:53
  • Oops! Actually my real issue was fixed with this: http://stackoverflow.com/questions/14775844/unicorn-triggers-mongoid-error-during-assets-precompile – Geoff Mar 27 '13 at 02:15
2

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.

mv_house
  • 51
  • 2
1

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);
}
S.M.Mousavi
  • 5,013
  • 7
  • 44
  • 59