Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: How to display image from rails local project folder?

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?

like image 348
Iggy Avatar asked Aug 21 '16 23:08

Iggy


People also ask

How do I show an image in Ruby on Rails?

Step 1 Import the Image File The goat image that will be added to the about page can be downloaded from this URL. To add the goat image to project, we save the image file in the project directory app/assets/images/ . Putting the image in this directory will make the image available for the app's webpages to display.

Where do you put images in rails?

To do this, go to app/assets/images and add all your file to the images folder. Though this is fine for small projects, you may want to better organize your image files if you're going to build a large project that can have hundreds of images.


1 Answers

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

like image 112
neydroid Avatar answered Sep 22 '22 16:09

neydroid