How to display image from rails local project folder?

14,727

Solution 1

I was browsing and saw this SO post. I was able to get it work using

<%= image_tag ("/assets/mockup/img2.jpg"), class: "img-responsive"%>

Image path was assets/images/mockup/img2.jpg; omitting images from the image path assets/mockup/img2.jpg solves the issue.

Solution 2

You have two options:

1) Move your img folder contents to app/assets/images and then reference the image like:

<%= image_tag ("mockup/img2.jpg", class: "img-responsive")%>

2) Add your img folder to the Rails assets path search in the file config/application.rb

config.assets.paths << Rails.root.join("app", "assets", "img")

This happens because app/assets/img/ is not included by default in the rails assets search path.

For more info check http://guides.rubyonrails.org/asset_pipeline.html#asset-organization

Share:
14,727
Iggy
Author by

Iggy

If when I was a lad I ate four dozen eggs. And now that I am grown I eat five dozen eggs. That's about (4 x 12 x 365 x 18) + (5 x 12 x 365 x 9) = 0.5 million eggs eaten.

Updated on June 13, 2022

Comments

  • Iggy
    Iggy almost 2 years

    Very basic question, but somehow I can't get it to work. I am trying to have an image located in project's local folder to display on Rails. Additionally, I am using bootstrap; thus I need to declare class: "img-responsive" as well. Here is the original code: <img class="img-responsive" src="assets/img/mockup/img2.jpg" alt="">

    I have consulted this post which suggested <%= image_tag("xyz.png", class: "img-responsive img-thumbnail img-circle") %> and rubyonrails guide which suggested image_tag("/icons/icon.gif", class: "menu_icon").

    I have tried

    <%= image_tag ("assets/img/mockup/img2.jpg", class: "img-responsive")%>
    

    and

    <%= image_tag "assets/img/mockup/img2.jpg" %>
    

    But it still does not work. I can confirm that the path is: app/assets/img/mockup/img2.jpg

    How can I display the said image on rails app view?

  • Iggy
    Iggy over 7 years
    I went to change the img to images and did <%= image_tag "/mockup/img2.jpg"%> as suggested on the link; the website loads but the image does not show. <%= image_tag ("/mockup/img2.jpg", class: "img-responsive") %> gives syntax error. <%= image_tag ("/mockup/img2.jpg"), class: "img-responsive"%> loads the page with no error, but the image still does not load.
  • Iggy
    Iggy over 7 years
    Solved it. Found my own solution. However, I appreciate the help! I learned something new from your #2. Thanks!