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

Ruby on Rails中图片无法加载的问题求助

Fixing Image Loading Issues in Rails (app/assets/images)

Hey there, I’ve run into this exact problem before— let’s get your images loading properly step by step.

First, let’s break down why you’re seeing the No route matches [GET] "/images/image.jpg" error: Rails’ asset pipeline doesn’t serve images directly from /images/ by default; it processes assets from app/assets/images and serves them from the /assets/ path instead. Here’s how to fix it:

  • Double-check your image’s location
    Make sure image.jpg is actually saved in app/assets/images/ (not a subfolder unless you specify it, like image_tag 'subfolder/image.jpg'). Also, watch out for filename capitalization— Rails is case-sensitive, so Image.jpg won’t match image.jpg.

  • Fix your configuration settings
    In newer Rails versions (4+), config.serve_static_assets has been replaced with config.public_file_server.enabled. Update your config/environments/development.rb file to:

    config.public_file_server.enabled = true
    config.assets.compile = true
    

    You don’t need to modify production.rb right now since you’re working locally.

  • Restart your Rails server
    This is crucial! Any changes to config files won’t take effect until you stop and restart your server (Ctrl+C then rails s again).

  • Use the correct image_tag syntax
    Your original <%= image_tag 'image.jpg' %> is the right approach— Rails should automatically generate the correct path (like /assets/image.jpg) once the pipeline is working. If you want to verify the asset path manually, you can use:

    <%= image_tag asset_path('image.jpg') %>
    

    Avoid hardcoding paths like <img src="/assets/images/image.jpg">— the asset pipeline handles fingerprinting and path resolution for you, so hardcoding can lead to broken links later.

  • Precompile assets (optional but helpful for development)
    Run this command in your terminal to precompile your assets into the public/assets folder:

    rails assets:precompile
    

    Then restart your server. This forces Rails to process and serve the compiled assets directly, which can resolve odd pipeline glitches.

If you’ve followed all these steps and still have issues, check if you have any custom routes that might be interfering with the /assets/ path— but in most cases, the above steps should get your images loading correctly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 04:20:02