Html 如何在 Rails 应用程序中将 <img src= > 转换为 image_tag

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39746936/
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-08-29 13:51:20  来源:igfitidea点击:

How to convert <img src= > to image_tag in rails app

htmlcssruby-on-railsruby

提问by Slowboy

This is my first post here and it might sound awfully stupid. Im building my first rails app.

这是我在这里的第一篇文章,听起来可能非常愚蠢。我正在构建我的第一个 Rails 应用程序。

I have this line in my index.html.erb

我有这条线 index.html.erb

    <img src="/assets/rand_front/<%= @random_image%>", style='height:50vw;width:100vw;margin-bottom:20px;' >

I want to use image_taginstead of the img src

我想用image_tag而不是img src

What is the correct way to wrap it around the code?

将它包装在代码周围的正确方法是什么?

So far I've tried <%= image_tag ( "/assets/rand_front/<%= @random_image%>", style='height:50vw;width:100vw;margin-bottom:20px;') %>

到目前为止我已经尝试过 <%= image_tag ( "/assets/rand_front/<%= @random_image%>", style='height:50vw;width:100vw;margin-bottom:20px;') %>

and <%= image_tag ( "/assets/rand_front/<%= @random_image%>"), style='height:50vw;width:100vw;margin-bottom:20px;' %>

<%= image_tag ( "/assets/rand_front/<%= @random_image%>"), style='height:50vw;width:100vw;margin-bottom:20px;' %>

and many other versions, but none seems to work, what am I doing wrong? and how should I write it properly?

和许多其他版本,但似乎都不起作用,我做错了什么?我应该如何正确编写它?

this <%= @random_image%>bit is taking this variable from the indexmethod in the controller.

<%= @random_image%>位从index控制器中的方法中获取此变量。

def index
   @products = Product.all.order(created_at: :desc).group_by(&:category_id)
    @images  = ["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg", "7.jpg", "8.jpg", "9.jpg", "10.jpg"]
    @random_no = rand(10)
    @random_image = @images[@random_no]
end

回答by Qaisar Nadeem

<%= image_tag ( "rand_front/#{@random_image}", style='height:50vw;width:100vw;margin-bottom:20px;') %>

image_tagwill automatically add assetsat start of the path

image_tag将自动添加assets到路径的开头

check Image Tagfor Documentation

检查图像标签以获取文档

回答by Black Enigma

If I am not mistaken you have a rand_frontfolder in your assets folder so you should call image_tag("#{@random_image}")since by default the image_taghelper should check all the folders in the assets directory for the image name

如果我没记错的话rand_front,你的资产文件夹中有一个文件夹,所以你应该调用,image_tag("#{@random_image}")因为默认情况下,image_tag助手应该检查资产目录中的所有文件夹的图像名称

For the CSS properties you can consider using the optionshash which would allow you to pass in the CSS properties as keys with your desired values

对于 CSS 属性,您可以考虑使用options散列,它允许您将 CSS 属性作为具有所需值的键传入

image_tag("#{@random_image}", height: 20, width: 20)You can check out the documentation in the previous answer

image_tag("#{@random_image}", height: 20, width: 20)您可以查看上一个答案中的文档

回答by jso1919

This is what Ruby on Rails is explicitly processing

这是 Ruby on Rails 显式处理的内容

 <%= image_tag("source", {:style => "width:100px;"}) %>

(meaning that Ruby allows you to use this code with out the ( { } ),

(这意味着 Ruby 允许您在不带 ( { } ) 的情况下使用此代码,

For me, first starting out its important to know that this is how Ruby on Rails actually running the code.

对我来说,首先要知道这是 Ruby on Rails 实际运行代码的方式,这一点很重要。

In other words YES you could leave the ( { } ) formality out because Ruby will understand your code, hope that helps to clarify some...

换句话说,是的,您可以省略 ( { } ) 形式,因为 Ruby 会理解您的代码,希望这有助于澄清一些...