premailer-rails: undefined method `find_asset' for nil:NilClass in asset_pipeline_loader

Today I updated the sprockets-rails gem which is now at 3.0.0 and noticed an error occurring after updating: “NoMethodError·undefined method `find_asset’ for nil:NilClass”. I’m using latest premailer-rails 1.8.2. The error is is happening here: https://github.com/fphilipe/premailer-rails/blob/master/lib/premailer/rails/css_loaders/asset_pipeline_loader.rb#L10

Noticed the issue reported on sprockets-rails https://github.com/rails/sprockets-rails/issues/237 back in April for the 3.0 version. I didn’t see what the fix was but it was mentioned that it was resolved in react-rails. I figure the same issue that happened there is probably happening here.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 21 (8 by maintainers)

Commits related to this issue

Most upvoted comments

I use sprockets-rails version > 3.0.0 and this line (requires gem compass-rails):

CompassRails.sprockets.find_asset(logical_file_path).to_s

instead of:

Rails.application.assets.find_asset(logical_file_path).to_s

and it works for me!

If you don’t use gem compass-rails you can get the asset with:

(Rails.application.assets || ::Sprockets::Railtie.build_environment(Rails.application)).find_asset(logical_file_path).to_s

@phlegx’s last solution works out-of-the-box, using Rails 4.2.1.

I’ve packaged it in a helper method in my app:

# app/helpers/application_helper.rb
# Returns the contents of the compiled asset (CSS, JS, etc) or an empty string
def asset_body(name)
   (Rails.application.assets || ::Sprockets::Railtie.build_environment(Rails.application)).find_asset(name).to_s
end

Still broken! This fix makes it so the assets can no longer be found in production. Looking at https://github.com/rails/sprockets-rails/issues/311, it seem this gem should use Rails.application.assets_manifest.assets[filename] to get the correct path to the compiled file in production.

A workaround for now (in the email template), works correctly in production and development:

link[rel='stylesheet' type='text/css'
  href="/assets/#{Rails.application.config.assets.compile ? 'email.css' : assets_manifest.assets['email.css']}"]