简体   繁体   English

Rails:如何将 class 添加到此图像 src 代码中? 将 div 标签放在它周围是行不通的

[英]Rails: how do I add a class to this image src code? Putting div tags around it didn't work

<% if item.image.attached? %>
    <image src="<%=(url_for(item.image), :class => "itemholsIm") %>">
<% end %>

How should it be done?应该怎么做?

You can use img_tag helper.您可以使用 img_tag 助手。

<%= image_tag(item.image, class: "itemholsIm") %>

You have to put the CSS class out of the <%= %>你必须把 CSS class 从 <%= %>

Try this:尝试这个:

<% if item.image.attached? %>
    <image src="<%=(url_for(item.image)%>" class="itemholsIm">
<% end %>

Yes, the rails img_tag helper is the best solution !是的,rails img_tag 助手是最好的解决方案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM