Gabriel Carneiro
Gabriel Carneiro

Reputation: 85

How can I render an image on Ruby on Rails?

I want to render an image after calling a method in my application. Verifying in my rails console the file_path to the image that I want to render I received the following return :

[23] pry(main)> y.profile_photo.file_path => "/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBFQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--6b614514bb24ee97ca27772d6aebbb2f767c04d7/seneca.jpeg"

Obs : y is a user with a role named driver

Then in .erb file I tried to pass this image to be rendered as :

<div> <img src="http:localhost:3000/#{<%[email protected]_photo.file_path%>}"></div>

But it did not work. How can I render this image? (located at) :

/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBFQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--6b614514bb24ee97ca27772d6aebbb2f767c04d7/seneca.jpeg

That's how it was rendered in the gmail

Above, That's how it was rendered in the gmail

Upvotes: 3

Views: 2791

Answers (1)

Ravi Teja Gadi
Ravi Teja Gadi

Reputation: 2135

In rails you can use image_tag to render images.

<%= image_tag @ride.driver.profile_photo.file_path %>

Or you can use html tag and render image in the src attribute.

 <img src="<%= @ride.driver.profile_photo.file_path %>">

You don't have to absolute path to render image src as browser will take of it.

Upvotes: 2

Related Questions