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
RAILS_ENV=production bundle exec rake assets:precompile
but no luck as well. Update:
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.
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