簡體   English   中英

在rails上的ruby中使用image_tag helper中的多個類

[英]Using multiple class in image_tag helper in ruby on rails

如何在Rails 5應用程序中將多個類傳遞給image_tag助手? 我想轉換此HTML <img>標記:

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

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

使用Rails image_tag幫助程序。 我該如何做到這一點?

雖然示例中的img不是有效的,但您可以嘗試:

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

由空格分隔的多個類。

您的HTML 無效 它應該是:

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

...否則第二class屬性會覆蓋第一class屬性。

可能的方法:

<% if @post.picture %>
  <%= image_tag @post.picture.url, class: "etalage_thumb_image img-responsive" %>
<% end %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM