3

How do I pass multiple classes to the image_tag helper in a Rails 5 app? I want to convert this HTML <img> tag:

<img class="etalage_thumb_image" src="images/m1.jpg" class="img-responsive" />

into

<%= image_tag @post.picture.url if @post.picture? %>

with the Rails image_tag helper. How do I accomplish this?

Mate Solymosi
  • 5,699
  • 23
  • 30
Shofi
  • 41
  • 5

2 Answers2

3

Although your img in the example isn't a valid one, you can try with:

<% if @post.picture %> # To check if @post.picture isn't nil
  <%= image_tag @post.picture.url, class: 'etalage_thumb_image img-responsive' %>
<% end %>

Multiple classes separated by a white space.

Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
2

Your HTML is invalid to begin with. It should be:

<img class="etalage_thumb_image img-responsive" src="images/m1.jpg" />

...otherwise the second class attribute overrides the first one.

Possible solution:

<% if @post.picture %>
  <%= image_tag @post.picture.url, class: "etalage_thumb_image img-responsive" %>
<% end %>
Mate Solymosi
  • 5,699
  • 23
  • 30