我有一个 rails 图像标签”
<%= image_tag 'how-it-works.png', {id: 'how-it-works'} %>
我还想添加 bootstrap 类
img-responsive
所以图像留在它的容器内。一个人会怎么做?
我在 rails docs 上看到了一些选项,但它们似乎都没有工作:
image_tag("icon")
# => <img alt="Icon" src="/assets/icon" />
image_tag("icon.png")
# => <img alt="Icon" src="/assets/icon.png" />
image_tag("icon.png", size: "16x10", alt: "Edit Entry")
# => <img src="/assets/icon.png" width="16" height="10" alt="Edit Entry" />
image_tag("/icons/icon.gif", size: "16")
# => <img src="/icons/icon.gif" width="16" height="16" alt="Icon" />
image_tag("/icons/icon.gif", height: '32', width: '32')
# => <img alt="Icon" height="32" src="/icons/icon.gif" width="32" />
image_tag("/icons/icon.gif", class: "menu_icon")
# => <img alt="Icon" class="menu_icon" src="/icons/icon.gif" />
image_tag("/icons/icon.gif", data: { title: 'Rails Application' })
# => <img data-title="Rails Application" src="/icons/icon.gif" />
最佳答案
image_tag(source, options = {})
source
是您在 assets/images
中的图像文件的名称目录,因此,您可以通过 id
和 class
作为选项。无需使用括号(第 6 个示例):<%= image_tag 'how-it-works.png', id: 'how-it-works', class: 'img-responsive' %>
其中呈现:
<img id="how-it-works" class="img-responsive" src="/assets/how-it-works-....png" alt="How it works">
关于ruby-on-rails - 将 id 和类添加到 rails 中的图像标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46703253/