Rails生产环境使用image_tag提示资源不在资源管道问题求助
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 namedHeroBanner.pngbut you’re callingimage_tag('herobanner.png'), it’ll fail. Make sure the path inimage_tagexactly matches the file structure underapp/assets/images/—subfolders count too. For example, if the image lives atapp/assets/images/icons/menu.png, useimage_tag('icons/menu.png').Confirm the asset was actually precompiled
After runningRAILS_ENV=production rails assets:precompile, head to thepublic/assetsdirectory. You should see your image with a hashed filename (likemenu-abc123def456.png) and a corresponding entry inpublic/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.
- To fix this, add the asset to the precompile list in
Validate your production.rb asset settings
Ensure these configurations are correct inconfig/environments/production.rb:config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?– ConfirmRAILS_SERVE_STATIC_FILESis actually set to"true"in your production environment. Test this by runningRAILS_ENV=production rails cand typingENV['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 frompublic/assetsdirectly 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:clobberto delete precompiled assets, then re-runassets:precompile. - Test in incognito mode or clear your browser cache to avoid loading stale asset paths.
- Run
RAILS_ENV=production rails cache:clearto wipe Rails’ internal cache.
- Run
Check for typos in asset references
It’s easy to miss a typo in the filename or path. Double-check that the file exists inapp/assets/images/with the exact name and structure you’re referencing inimage_tag.
内容的提问来源于stack exchange,提问作者Vega180

