3

如何image_tag在 Rails 5 应用程序中将多个类传递给助手?我想转换这个 HTML<img>标签:

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

进入

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

使用 Railsimage_tag助手。我该如何做到这一点?

4

2 回答 2

3

尽管您img在示例中不是有效的,但您可以尝试:

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

由空格分隔的多个类。

于 2017-07-09T23:05:50.307 回答
2

您的 HTML 一开始就无效。它应该是:

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

...否则第二个class属性会覆盖第一个属性。

可能的解决方案:

<% if @post.picture %>
  <%= image_tag @post.picture.url, class: "etalage_thumb_image img-responsive" %>
<% end %>
于 2017-07-09T23:06:43.150 回答