简体   繁体   English

Rails:如何转换<img src= >到 rails 应用程序中的 image_tag

[英]Rails: How to convert <img src= > to image_tag in rails app

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_tag instead 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;' %> <%= 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 index method 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
<%= image_tag "rand_front/#{@random_image}", style: 'height:50vw;width:100vw;margin-bottom:20px;' %>

image_tag will automatically add assets at start of the path image_tag将在路径的开头自动添加assets

check Image Tag for Documentation检查图像标签以获取文档

If I am not mistaken you have a rand_front folder in your assets folder so you should call image_tag("#{@random_image}") since by default the image_tag helper 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 options hash 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)可以查看上一个回答中的文档

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 会理解您的代码,希望这有助于澄清一些...

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

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