Rails sprockets 3.0 find_asset

762 Views Asked by At

I used to use rails-sprockets 2.x and was using the following in an email layout template to include the css

Rails.application.assets.find_asset('file').to_s.html_safe

However, this no longer works in rails-sprockets 3.x and it is suggested to use

Rails.application.assets_manifest.assets['file.css']

This only returns the string name of the file (if it exists). How would I get the body of the file so I can output it in the view?

1

There are 1 best solutions below

0
On

I just ran into this issue too, although I upgraded to sprockets 3.x about 7 months.

I threw this together as quick as I could (there is likely a better solution out there...) - this will get you the path name - just use File.read()

def find_asset_path(asset_name)
  if Rails.application.assets
    Rails.application.assets.find_asset(asset_name).pathname
  else
    name = Rails.application.assets_manifest.assets[asset_name]
    File.join(Rails.public_path, 'assets', name)
  end
end