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?
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.
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 %>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.