简体   繁体   English

使用IF语句将类添加到Rails中的image_tag

[英]Add class to image_tag in Rails with IF statement

I have a image tag setup for a blog that if an image has been attached to the blog then display the image. 我有一个博客的图像标签设置,如果博客中已附加了图像,则显示该图像。

<%= image_tag blog.image_url(:thumb) if blog.image? %>

I am trying to add a class to this image tag so that I can use CSS to edit it. 我正在尝试向该图像标签添加一个类,以便可以使用CSS对其进行编辑。 I have tried a few different ways to add the class but none seem to work. 我尝试了几种添加类的方法,但是似乎都没有用。

<%= image_tag blog.image_url(:thumb) if blog.image?, :class => "img" %>

or 要么

<%= image_tag (blog.image_url(:thumb), :class => "img") if blog.image? %>

or 要么

<%= image_tag (blog.image_url(:thumb) if blog.image?), :class => "img" %>

does anyone know how to add a class to this type of statement? 有谁知道如何向此类语句添加类?

<%= image_tag(blog.image_url(:thumb), class: 'img') if blog.image? %>

I use <%= image_tag(@user.image_url, size: 50, class: 'gravatar') if @user.image_url? %> 我使用<%= image_tag(@user.image_url, size: 50, class: 'gravatar') if @user.image_url? %> <%= image_tag(@user.image_url, size: 50, class: 'gravatar') if @user.image_url? %> in my project, and it works. <%= image_tag(@user.image_url, size: 50, class: 'gravatar') if @user.image_url? %>在我的项目中,并且有效。

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

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