sprockets-rails: Rails.application.assets is nil in production
Now in sprockets-rails
3 app.assets
is set only in case when assets.compile
option is enabled (https://github.com/rails/sprockets-rails/blob/master/lib/sprockets/railtie.rb#L142).
By default in Rails this option is set to false
in production environment: https://github.com/rails/rails/blob/master/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt#L27
This leads us to the bug when some gem calls Rails.application.assets.resolve("asset.js")
in production environment and the NoMethodError is raised.
@josh does it sounds like a bug?
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 21 (7 by maintainers)
Commits related to this issue
- Fix compatibility issue with Sprockets Rails 3.0.0 Sprockets Rails now unset `Rails.application.assets` when `config.assets.compile` is false, causing an issue as we were relying on that hash. The wo... — committed to sikachu/sprockets-redirect by sikachu 8 years ago
- Fix compatibility issue with Sprockets Rails 3.0.0 Sprockets Rails now unset `Rails.application.assets` when `config.assets.compile` is false, causing an issue as we were relying on that hash. The wo... — committed to sikachu/sprockets-redirect by sikachu 8 years ago
- Fix compatibility issue with Sprockets Rails 3.0.0 Sprockets Rails now unset `Rails.application.assets` when `config.assets.compile` is false, causing an issue as we were relying on that hash. The wo... — committed to sikachu/sprockets-redirect by sikachu 8 years ago
- fixes #240 - issue caused by upgrade sprockets-rails to 3.0.0 Rails.application.assets is nil in production https://github.com/rails/sprockets-rails/issues/237 Rails.application.assets is Nil on Her... — committed to din-co/d2 by beausmith 8 years ago
- Specify full path for css file — committed to prakashmurthy/princely by prakashmurthy 8 years ago
- Fix mailer generation in production Followed suggestion at https://github.com/rails/sprockets-rails/issues/237#issuecomment-231940623 — committed to CityOfBoston/successlink-frontend by mzagaja 7 years ago
- Fix stylesheet inclusion According to this comment (https://github.com/rails/sprockets-rails/issues/237#issuecomment-273398103), the committed changed here is the exact replacement for the previously... — committed to CityOfBoston/successlink-frontend by allthesignals 7 years ago
- Updates modernizr include to handle Sprockets upgrade from 2 to 3 In sprockets v3, when running the rails application in production mode, `Rails.application.assets` returns nil (see: https://github.c... — committed to moneyadviceservice/rad_consumer by scott-ad-riley 6 years ago
- Try to get mailer css working again This will compile every time while sending the mail which is not ideal but at least this appears to work after upgrading to rails 5 https://github.com/rails/sprock... — committed to openaustralia/planningalerts by mlandauer 6 years ago
- Corrige le helper `svg_tag_base64` en production (sans l'asset pipeline) En production, quand l'asset pipeline est désactivé (config.assets.compile = false), les assets peuvent être retrouvés avec la... — committed to betagouv/eva-serveur by etienneCharignon 4 years ago
- Corrige le helper `svg_tag_base64` en production (sans l'asset pipeline) En production, quand l'asset pipeline est désactivé (config.assets.compile = false), les assets peuvent être retrouvés avec la... — committed to betagouv/eva-serveur by etienneCharignon 4 years ago
There is a method to that in sprocket-rails.
Rails.application.assets_manifest.find_sources
Just as the reference for people trying to fix it in the local codebase after the update. The easy fix is to replace
Rails.application.assets.find_asset("#{asset_name}")
withRails.application.assets_manifest.files.values.map{|v| v['logical_path']}.include?('#{asset_name}')
We needed to access a compiled file within another file during precompilation, so
Rails.application.assets_manifest
didn’t work the first time you runassets:precompile
.We solved this by doing this:
In case anyone is in the same situation that we are.
Thanks @rafaelfranca. Rails.application.assets_manifest.find_sources(‘asset.js’).first seems to return exactly what Rails.application.assets[‘asset.js’].to_s used to produce and it works both in dev and prod 😃
@nasa42
Rails.application.assets_manifest.find(name).first
gives the error:“Sprockets::Error: manifest requires environment for compilation” which makes it sound like it’s trying to recompile…
If you are looking for an exact replacement for
asset = Rails.application.assets.find_asset(name)
, (i.e., a method that returns aSprockets::Asset
object, so that you can have access to methods likecontent_type
), useasset = Rails.application.assets_manifest.find(name).first
.My workaround suggestion is to make your own helper in ApplicationController, which will use application.assets or application.assets_manifest as needed. Because the manifest will be empty when using the debug asset pipeline, whereas the debug .assets hash will be nil on production. And remember to use File.binread to ensure images don’t get broken.
I found ways to check for assets in both development and production (heroku) in Rails 5.2, but neither works in both environments, so I added an application helper like this:
Now I can check if an image_exists in both production and development before calling image_tag. The root of the problem is image_tag throws an exception if the image doesn’t exist…why not just respond nil???
ISTM the ideal would be for us to provide an
application.assets
that knows how to look things up from the manifest, without doing any new compilation.That way we could help people ignore implementation details (that, IMO, they shouldn’t need to care about) like where on the filesystem the assets actually live – basically providing a more-suitably-shaped API around just using File.read.