Rails 3.1: Access compiled assets

Posted by Moser on 16 Sep 2011

I use wicked pdf for PDF generation in my rails app foxtrot mike. Wicked pdf has a helper method which copies the content of CSS files into the generated HTML. This does not work in Rails 3.1 because CSS files are generated by Sprockets. Fortunately it is possible to access the compiled asset files. When the asset pipeline is enabled, the application object has an attribute assets which returns a Sprockets::Environment.

The following code includes the contents of application.css in my layout which is used for PDF generation.

  %style{ :type => "text/css" }
    = Rails.application.assets['application.css'].to_s.html_safe