Ruby-on-rails 如何将类添加到 image_tag rails helper

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8273410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:24:21  来源:igfitidea点击:

How to add class to an image_tag rails helper

ruby-on-railsimagehelperlccs

提问by content01

I have the following code:

我有以下代码:

<%= image_tag iterator.image.url(:small), :class => "img_preview" %>

But the rendered HTML shows:

但呈现的 HTML 显示:

<img src="/actives/hotels/13/small/clean_wave.jpg?1317675452" alt="Clean_wave">

Why the "class" attribute isn't there?

为什么“class”属性不存在?

Thank you!

谢谢!

回答by Simpleton

Your class has to be assigned inside of the brackets to be used as part of the options being passed through. Try:

您的类必须在方括号内分配,以用作传递的选项的一部分。尝试:

<%= image_tag(iterator.image.url(:small), :class => "img_preview") %>

回答by Brandon LoGuercio

For newbies like myself, heres a cleaner version with the newer rails syntax:

对于像我这样的新手,这里有一个带有较新 rails 语法的更简洁的版本:

<%= image_tag iterator.image.url(:small), class:"img_preview" %>