Shofi
Shofi

Reputation: 41

Using multiple class in image_tag helper in ruby on rails

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?

Upvotes: 3

Views: 703

Answers (2)

Sebasti&#225;n Palma
Sebasti&#225;n Palma

Reputation: 33481

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.

Upvotes: 3

Mate Solymosi
Mate Solymosi

Reputation: 5977

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 %>

Upvotes: 2

Related Questions