Follow Along as I Stumble on the Path to Learning RoR

Posts tagged “asset pipeline

Silencing Asset Pipeline Details in Rails Logs

The asset pipeline details can get quite extensive in Rails logging and since the details you are analyzing typically are not related to the loading of assets, you may want to silence them, or prevent them from logging – especially since you can always turn logging back on if you run into asset troubles.

I’ve found three ways to do so:

  1. you can use the Quiet Assets gem

These other two were mentioned on Stack Overflow, but I was not able to get them to work:

  1. you can add this as an initializer in config/initializers/quiet_assets.rb:
if Rails.env.development?
  Rails.application.assets.logger = Logger.new('/dev/null')
  Rails::Rack::Logger.class_eval do
    def call_with_quiet_assets(env)
      previous_level = Rails.logger.level
      Rails.logger.level = Logger::ERROR if env['PATH_INFO'] =~ %r{^/assets/}
      call_without_quiet_assets(env)
    ensure
      Rails.logger.level = previous_level
    end
    alias_method_chain :call, :quiet_assets
  end
end

3.  if you are using Rails 3.2 or above, you can include this setting in config/environments/development.rb:

config.assets.logger = false

Advertisements

Error From Coffee Files

When I had to set up a new Windows PC with Rails, I ran in to a mysterious error where I couldn’t view my app because of purported problems with my .coffee.js files.  However, I knew there was no issue with the files since they were created during scaffolding and had only comments in them.  Furthermore, I was able to run the same code on my other PC fine, as is.

When I viewed the –trace there was nothing of note (except listing that the coffee files had issues with them).  When I tried to load the view, I did see this though “ExecJS::RuntimeError in Home#index”

Fortunately, I found the solution in this thread which explains that this is the result of Node.js not on a PC, or the path not set correctly in Windows environment variables.  I downloaded the Windows installer (.msi) file and installed Node.js and added the path to the PATH environment variable.  After I restarted my Rails server, the error went away.


Another Permission Error with Assets Precompile

I was receiving an error message indicating that I didn’t have access to ‘root@localhost’ when I was trying to run rake assets:precompile.  I tried using the chown command, but that didn’t work.

I found out that in order for the precompile command to work I needed ‘config.assets.initialize_on_precompile=false  ‘ in my application.rb file.


Referencing an Image from CSS File in Asset Pipeline

In order to reference an image from the CSS file, I added .scss at the end of the .css file and referenced the image using this syntax:

background: url(asset-path(“example.png”,image)) no-repeat; 


Making the Asset Pipeline Less Verbose

I was getting a little overwhelmed by the amount of messages regarding each asset in my development logs, so I wanted to make my logs less verbose.  It was making it more challenging to find the real actions and queries I was trying to analyze.

I found the answer in this post on Stack Overflow.

For some people, adding the initializer code worked, but for me, I went with adding:

config.assets.debug = false

in config/enviroments/development.rb 

and that was sufficient for me.  I’m using Rails 3.1 and I noticed different settings people used in 3.2, so it may also be different if you use 3.2.


Fixing Permission Denied Error Compiling Assets

I started getting Permission Denied errors when running rake assets:precompile. Luckily I found this command from a post on Stackoverflow

(from the root directory in the command window):

chown [username] .

don’t forget the period.


Getting Font-Face to work with the Asset Pipeline

I use a custom font-face (from font squirrel) which is referenced in my CSS and I also have the font files stored within my app.  When I implemented the asset pipeline, the font-face was not read in because my fonts were not referenced correctly. After a lot of trial and error I found my answer.

(1) I had to install the sass-rails gem in my gemfile (https://github.com/rails/sass-rails)

(2) I needed to change my .css file to .css.sccs (http://guides.rubyonrails.org/asset_pipeline.html) to allow sass processing of the files (which allows the use of the sass helper methods ‘asset-path’ and ‘asset-url’ – see https://github.com/rails/sass-rails for helpers).  As a sidenote, it is recommended that for sass-rails you use @import (see gem page on github) instead of requirerequire_tree, and require_self (I left require_self) 

(3) I placed my fonts in app/fonts

(4) this post on Stack Overflow (http://stackoverflow.com/questions/7973271/using-font-face-with-rails-3-1-app) by Greg Stewart was how I needed to adjust my CSS (below).  

@font-face {
    font: {
       family: 'Junction';
       weight: 'normal';
       style: 'normal';
    }
    src: asset-url('Junction-webfont.eot', font);
    src: asset-url('Junction-webfont.eot', font) format('embedded-opentype'),
         asset-url('Junction-webfont.woff', font) format('woff'),
         asset-url('Junction-webfont.ttf', font) format('truetype'),
         asset-url('Junction-webfont.svg#JunctionRegular', font) format('svg')
}

(5) you may need to precompile your assets to get this to work – depending on if your app has config in application.rb as precompile = true (or false).  to precompile use this command:

bundle exec rake assets:precompile

(6) If you have trouble after this, you may need to add the fonts to the assets path by adding this to your application.rb file: 

config.assets.paths << "#{Rails.root}/app/assets/fonts"