12

I am using fontawesome 3.2.1 and bootstrap 3.0.0 in my rails 4.0.1 project. All my assets are located in vendor/assets.

the problem is that my fontawesome is working in development mode when when i compile my assets(production env) and run the server in production env, its not able to load fontawesome. the errors are as

Started GET "/assets/fontawesome-webfont.svg" for 127.0.0.1 at 2014-01-08 11:48:55 +0530

ActionController::RoutingError (No route matches [GET] "/assets/fontawesome-webfont.svg"):
 actionpack (4.0.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
 actionpack (4.0.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'

the assets are as

$ls vendor/assets/
=> fonts  images  javascripts  stylesheets

$ls vendor/assets/*
=> vendor/assets/fonts:
FontAwesome.otf          fontawesome-webfont.ttf           glyphicons-halflings-   regular.svg
fontawesome-webfont.eot  fontawesome-webfont.woff          glyphicons-halflings- regular.ttf
fontawesome-webfont.svg  glyphicons-halflings-regular.eot  glyphicons-halflings-regular.woff

vendor/assets/images:
bg_direction_nav.png  bxslider  search-icon.jpg

vendor/assets/javascripts:
bootstrap  bxslider  fancybox  others  revolution_slider

vendor/assets/stylesheets:
bootstrap  bxslider  fancybox  font_awesome  others  revolution_slider

 $ls vendor/assets/stylesheets/bootstrap/
 => bootstrap.min.css

 $ls vendor/assets/stylesheets/font_awesome/
 => font-awesome.css

my application.css is as

$cat app/assets/stylesheets/application.css 
/*
* This is a manifest file that'll be compiled into application.css, which will include   all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the top of the
* compiled file, but it's generally better to create a new file per style scope.
*
*= require bootstrap/bootstrap.min.css
*= require others/theme.css
*= require others/bootstrap-reset.css
*= require font_awesome/font-awesome.css
*= require bxslider/jquery.bxslider.css
*= require fancybox/jquery.fancybox.css
*= require revolution_slider/rs-style.css
*= require revolution_slider/settings.css
*= require others/flexslider.css 
*= require others/style.css
*= require others/style-responsive.css
*= require_self
*/

fontawesome are loaded in font-awesome.css as

@font-face {
  font-family: 'FontAwesome';
  src: url('/assets/fontawesome-webfont.eot?v=3.2.1');
  src: url('/assets/fontawesome-webfont.eot?#iefix&v=3.2.1') format('embedded-opentype'), url('/assets/fontawesome-webfont.woff?v=3.2.1') format('woff'), url('/assets/fontawesome-webfont.ttf?v=3.2.1') format('truetype'), url('/assets/fontawesome-webfont.svg#fontawesomeregular?v=3.2.1') format('svg');
  font-weight: normal;
  font-style: normal;
}

glyphicons are loaded in bootstrap.min.css as

@font-face{
  font-family:'Glyphicons Halflings';
  src:url('/assets/glyphicons-halflings-regular.eot');
  src:url('/assets/glyphicons-halflings-regular.eot?#iefix') format('embedded-Opentype'), url('/assets/glyphicons-halflings-regular.woff') format('woff'),url('/assets/glyphicons-halflings-regular.ttf') format('truetype'),url('/assets/glyphicons-halflings-regular.svg#glyphicons-halflingsregular') format('svg')}

i did try couple of solution like prepending 'font' or 'assets' to 'url' but none worked.

--UPDATE

contents of config/application.rb

config.assets.enabled = true
config.assets.version = '1.0'

config.assets.paths += ["#{config.root}/vendor/assets/fonts", "#config.root}/app/assets/images/**", "#{config.root}/vendor/assets/images"]
config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif *.eot *.svg *.ttf *.otf *.woff vendor/assets/stylesheets/**/* vendor/assets/fonts/*)

["#{config.root}/vendor/assets/javascripts", "#config.root}/vendor/assets/stylesheets"].each do |d|
config.assets.precompile += Dir.glob("#{d}/*").map{|f| "#{f.gsub(d + '/', '')}/**/*" if File.directory?(f)}.compact
Prasad Surase
  • 6,486
  • 6
  • 39
  • 58

2 Answers2

10

I solved the problem as specified below.

1) Firstly, dont refer assets inside .css file. Rename the file to .css.scss. Thats the same extension whenever u create controller and it creates assets for that controller.(javascript and css). There are multiple applications to convert css to scss. Use them if the css is large.

2) use font-path whenever you want to refer a font inside a stylesheet. ie. use methods provided by rails to refer to assets rather than using 'src' eg

@font-face {
  font-family: 'MuseoSans-700';
  src: url(font-path('museoSans/244DD4_0_0.eot'));
  src: url(font-path('museoSans/244DD4_0_0.eot?#iefix')) format('embedded-opentype');
  src: url(font-path('museoSans/244DD4_0_0.woff')) format('woff');
  src: url(font-path('museoSans/244DD4_0_0.ttf')) format('truetype');
}

In the above code, my file is as "app/assets/stylesheets/fonts.scss" and my fonts are located in

$ls vendor/assets/fonts/
  fontawesome  museoSans  museoSans500
$ls vendor/assets/fonts/museoSans
  244DD4_0_0.eot   244DD4_1_0.ttf   244DD4_2_0.woff  244DD4_4_0.eot   244DD4_5_0.ttf   244DD4_6_0.woff  244DD4_8_0.eot   244DD4_9_0.ttf
  244DD4_0_0.ttf   244DD4_1_0.woff  244DD4_3_0.eot   244DD4_4_0.ttf   244DD4_5_0.woff  244DD4_7_0.eot   244DD4_8_0.ttf   244DD4_9_0.woff
  244DD4_0_0.woff  244DD4_2_0.eot   244DD4_3_0.ttf   244DD4_4_0.woff  244DD4_6_0.eot   244DD4_7_0.ttf   244DD4_8_0.woff
  244DD4_1_0.eot   244DD4_2_0.ttf   244DD4_3_0.woff  244DD4_5_0.eot   244DD4_6_0.ttf   244DD4_7_0.woff  244DD4_9_0.eot

3) During asset precompilation, the helpers like 'font-path' or 'asset-path' help in pointing to the right asset. ie. if u specify it as

src: url('/assets/museoSans/244DD4_0_0.eot'));

this is still going to be the same in the compiled asset. it should ideally be

src: url('/assets/museoSans/244DD4_0_0-67652745236457645234dghfhsagfd64354.eot'));

You can 'grep' and check out the compiled file in "public/assets".

4) another way of making sure the assets are loading is the to call them from url.

5) Please set 'config.assets.compress = false' in your production or staging env file and run the application locally in prod/staging env so as the check the views to make sure that the assets are loading.

You can checkout https://github.com/joshsoftware/website/commit/859f2709180e9fb0aac59549d64bd4351a2842b3 commit for more understanding.

Prasad Surase
  • 6,486
  • 6
  • 39
  • 58
  • 1
    I was trying to fix Bootstrap glyph icons (for over 2 hours), but `font-path` was the trick to finally make the font definition override in application.css start working in production. – David Krider Feb 28 '16 at 23:29
  • @DavidKrider rename application.css to application.scss. – Prasad Surase Mar 03 '16 at 05:35
5

Your svg doesn't seem to be compiled, the fingerprint seems missing except if you set:

config.assets.digest = false 

I was getting the same " no routes match GET" after precompiling. you can try to add the svg here in your config/application.rb:

config.assets.precompile += %w(*.svg)

And then retry a:

rake assets:precompile RAILS_ENV=production

In my case a simple rake assets:precompile wasn't enough i needed to specify the RAILS_ENV and RAILS_GROUPS (If the version of rails is lower than 4).

Normally the compiled assets are found in the public/assets directory.

I hope it helps

UPDATE:

Another idea coming from my experience with ckeditor with rails 4 in production. The assets compilation with digest is not working (https://github.com/galetahub/ckeditor). You need to add a rake task that copies/modify some files in the public/assets directory after compilation. Here is the code:

namespace :ckeditor do
  desc 'Create nondigest versions of some ckeditor assets (e.g. moono skin png)'
  task :create_nondigest_assets do
    fingerprint = /\-[0-9a-f]{32}\./
    for file in Dir['public/assets/ckeditor/contents-*.css', 'public/assets/ckeditor/skins/moono/*.png']
      next unless file =~ fingerprint
      nondigest = file.sub fingerprint, '.' # contents-0d8ffa186a00f5063461bc0ba0d96087.css => contents.css
      FileUtils.cp file, nondigest, verbose: true
    end
  end
end

# auto run ckeditor:create_nondigest_assets after assets:precompile
Rake::Task['assets:precompile'].enhance do
  Rake::Task['ckeditor:create_nondigest_assets'].invoke
end

You can easily replace ckeditor with fontawsome.

coding addicted
  • 3,422
  • 2
  • 36
  • 47
  • 'assets' group has been depricated from rails 4. – Prasad Surase Jan 13 '14 at 17:15
  • Indeed, but in my case as i mentioned a simple rake assets:precompile was not enough ( some bootstrap elements and ckeditor as i remember were not precompiled ). Maybe specifying the ENV is enough but i can't explain why this difference occurs. Have you try to add config.assets.precompile += %w(*.svg) in your config/application.rb? – coding addicted Jan 13 '14 at 18:51
  • yes. i have specified it in the precompile settings. what i dont understand is why glyphicons are loading and not fontawesome. i mean, they have the same extensions, and are located in the same directory. – Prasad Surase Jan 14 '14 at 06:07
  • In my case the last time i get this no route match on my assets the compilation was missing some files. You can check the manifest to see if fontawsome is well compiled (public/assets/manifest-fingerprint.json). I have another idea i'm updating my answer. – coding addicted Jan 14 '14 at 11:30
  • Another question, do use a gem for fontawesome? If yes would you mind sharing a link so i can give it a try in a blank project? – coding addicted Jan 14 '14 at 11:48
  • i will create a blank project with just the minimum data n post the git repo url here. – Prasad Surase Jan 14 '14 at 12:03
  • I'm still on your question (sorry for the harassment). I just read another post on fontawsome an it suggests to rename font-awesome.css to font-awesome.css.erb and modify it. It will let rails handle the path whatever the environnement is. This can be a solution too. The post is http://stackoverflow.com/questions/11052398/rails-using-font-awesome – coding addicted Jan 14 '14 at 12:05