Rails.application.assets is Nil on Heroku after upgrade sprockets-rails to 3.0.0

瘦欲@ 提交于 2020-01-06 20:03:15

问题


I check this command Rails.application.assets in local console it work but in heroku console it return Nil.

but when I rollback to use sprockets-rails 2.3.3 and check Rails.application.assets in heroku console it return value.

what happened?


回答1:


This was the result of a change in sprockets-rails 3. See the issue https://github.com/rails/sprockets-rails/issues/237

You can get around the issue for now by setting config.assets.compile = true in /config/environments/production.rb




回答2:


You might need to change some of your code to be compatible with sprockets-rails 3, please read https://github.com/rails/sprockets/blob/master/UPGRADING.md




回答3:


I solved this by adding an application helper like this:

def image_exists?(path)
    if Rails.env == "development" && Rails.application.assets.find_asset(path)
      return true
    elsif Rails.env == "production" && Rails.application.assets_manifest.assets[path]
      return true
    else
      return false
    end
 end

Then I could check if an image exists by finding the path and running

image_exists?(path)

In my app, usually the path is just the image file name, unless the image is in a subfolder within the images directory.



来源:https://stackoverflow.com/questions/34380753/rails-application-assets-is-nil-on-heroku-after-upgrade-sprockets-rails-to-3-0-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!