You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails生产环境使用image_tag提示资源不在资源管道问题求助

Troubleshooting "Asset is not present in asset pipeline" in Rails Production

Let’s walk through targeted fixes for this issue—you’ve already covered the basics, so let’s dig into the most likely culprits that might have slipped through:

  • Double-check path accuracy and case sensitivity
    Rails’ asset pipeline is case-sensitive in production (unlike development). If your image is named HeroBanner.png but you’re calling image_tag('herobanner.png'), it’ll fail. Make sure the path in image_tag exactly matches the file structure under app/assets/images/—subfolders count too. For example, if the image lives at app/assets/images/icons/menu.png, use image_tag('icons/menu.png').

  • Confirm the asset was actually precompiled
    After running RAILS_ENV=production rails assets:precompile, head to the public/assets directory. You should see your image with a hashed filename (like menu-abc123def456.png) and a corresponding entry in public/assets/manifest.json. If the image isn’t there, it wasn’t included in the precompile process.

    • To fix this, add the asset to the precompile list in config/initializers/assets.rb:
      Rails.application.config.assets.precompile += %w( icons/menu.png )
      
    • Re-run the precompile command and verify the file appears in public/assets.
  • Validate your production.rb asset settings
    Ensure these configurations are correct in config/environments/production.rb:

    • config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present? – Confirm RAILS_SERVE_STATIC_FILES is actually set to "true" in your production environment. Test this by running RAILS_ENV=production rails c and typing ENV['RAILS_SERVE_STATIC_FILES']—it should return "true".
    • If you’re still stuck, temporarily set config.assets.compile = true (note: this is not ideal for long-term production use, as it compiles assets on the fly and impacts performance). If this fixes the error, it means some assets weren’t precompiled correctly—circle back to the precompile list step to add missing files.
  • Make sure your server serves static files properly
    If you’re using Nginx, Apache, or another reverse proxy, confirm it’s configured to serve files from public/assets directly instead of passing requests to Rails. For Nginx, your config should include something like:

    location /assets/ {
      root /path/to/your/app/public;
      expires 1y;
      add_header Cache-Control public;
    }
    

    If the server isn’t handling assets, Rails will try to route them as controller actions, triggering the "not present" error.

  • Clear cached assets and browser cache
    Old cached data can cause mismatches between the manifest and what’s served:

    • Run RAILS_ENV=production rails assets:clobber to delete precompiled assets, then re-run assets:precompile.
    • Test in incognito mode or clear your browser cache to avoid loading stale asset paths.
    • Run RAILS_ENV=production rails cache:clear to wipe Rails’ internal cache.
  • Check for typos in asset references
    It’s easy to miss a typo in the filename or path. Double-check that the file exists in app/assets/images/ with the exact name and structure you’re referencing in image_tag.


内容的提问来源于stack exchange,提问作者Vega180

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:34:57