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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Multiple classes are separated by a space, not a comma.
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 %>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.