Slowboy Slowboy - 3 years ago 258
HTML Question

How to convert <img src= > to image_tag in rails app

This is my first post here and it might sound awfully stupid. Im building my first rails app.

I have this line in my


<img src="/assets/rand_front/<%= @random_image%>", style='height:50vw;width:100vw;margin-bottom:20px;' >

I want to use
instead of the
img src

What is the correct way to wrap it around the code?

So far I've tried
<%= image_tag ( "/assets/rand_front/<%= @random_image%>", style='height:50vw;width:100vw;margin-bottom:20px;') %>

<%= image_tag ( "/assets/rand_front/<%= @random_image%>"), style='height:50vw;width:100vw;margin-bottom:20px;' %>

and many other versions, but none seems to work, what am I doing wrong? and how should I write it properly?

<%= @random_image%>
bit is taking this variable from the
method in the controller.

def index
@products = Product.all.order(created_at: :desc).group_by(&:category_id)
@images = ["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg", "7.jpg", "8.jpg", "9.jpg", "10.jpg"]
@random_no = rand(10)
@random_image = @images[@random_no]

Answer Source
<%= image_tag ( "rand_front/#{@random_image}", style='height:50vw;width:100vw;margin-bottom:20px;') %>

image_tag will automatically add assets at start of the path

check Image Tag for Documentation

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download