Search code examples
ruby-on-railsrubyrake

ActionController::RoutingError (No route matches [GET] "/assets/images


I'm trying to load some static pages and files (images, css and javascripts) in my rails application for testing. But no image is loaded nor bootstrap rendering. I have attempted different options as looked up on Stackoverflow as below before running on it on the server (with rails server)

  • use the command rake assets:precompile and use 'rake tmp:clear' or manually delete stuff inside the tmp directory

  • change the HTML syntax of the image tag to open different images under the resource directory ../../assets/images/hero-images/*.jpg like:

Before: <img src="../../assets/images/hero-images/abyssal_underlord_sb.png">

Now: <%= image_tag("../../assets/images/hero-images/abyssal_underlord_sb.png") %>

The ../.. basically tells me that I have to go back to the app directory to access the images folder, but it still doesn't work

  • As there's a reason with the production environment settings that I should run it on the setting, tried RAILS_ENV=production bundle exec rake assets:precompile but no luck as well.

Update:

  • Lastly, try to fix the configuration in the config directory. I have this line config.serve_static_assets = true in the development.rb file in the app (and even try rake assets:precompiles but still no luck.

Here's the snapshot of the part of my directory tree to refer for helping me troubleshoot these problems. enter image description here


Solution

  • You can render the images which located at inner folder of assets/images by 2 ways.

    1 Add following in application.rb

    config.assets.paths << Rails.root.join('app', 'assets', 'hero-images')
    
    Dir.glob("#{Rails.root}/app/assets/images/**/").each do |path|
      config.assets.paths << path
    end
    

    Access the image directly

    <img src="assets/hero-images/abyssal_underlord_sb.png">
    <%= image_tag("abyssal_underlord_sb.png")%>
    

    2. Simply add inner folder name above the file name.

    <%= image_tag("hero-images/abyssal_underlord_sb.png")%> 
    

    Refer to this link https://learn.co/lessons/images-and-the-asset-pipeline