`undefined method `find_asset' for nil:NilClass`
Asked Answered
G

2

12

I am using 'rails', '4.2.5' and wicked_pdf gem for generating and downloading PDF, but on Heroku, it's not including css using wicked_pdf_stylesheet_link_tag tag.

Heroku error log:

ActionView::Template::Error (undefined method 'find_asset' for nil:NilClass) 

Did I miss any configuration here?

Goldoni answered 7/2, 2016 at 9:23 Comment(0)
D
51

To get it work for development and production environment don't use

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

for sprockets-rails version > 3.0.0 but use this (requires gem compass-rails):

CompassRails.sprockets.find_asset(logical_file_path).to_s

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
Dimerous answered 14/6, 2016 at 19:34 Comment(1)
If you use the statements above to check if an asset exists, remove the little to_s in the end because nil.to_s = "" = trueFaithless
C
6

A simple Google search, revealed that this error was reported in the official wicked_pdf repo in December.

Setting assets.compile = true as recommended in the issue would fix it. This change should not be necessary with sprocket-rails != 3.0.0

Capri answered 7/2, 2016 at 9:25 Comment(3)
Have you made the changes and restarted the app?Capri
Set that where?Physiology
This is never recommended for production! It's a terrible solution and should only be a last resort. Re-emphasized here: guides.rubyonrails.org/asset_pipeline.html#local-precompilationHarman

© 2022 - 2024 — McMap. All rights reserved.