如何将转换为rails应用中的image_tag ?

时间:2021-12-18 00:27:01

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_tagem代替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;

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%>位从控制器的索引方法中提取该变量。

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

3 个解决方案

#1


4  

<%= 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将在路径的开始自动添加资产

check Image Tag for Documentation

检查图像标签的文档

#2


1  

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属性,您可以考虑使用选项散列,这将允许您将CSS属性作为键传递给您所需的值。

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

image_tag(“#{@random_image}”,高度:20,宽度:20)您可以查看前面答案中的文档

#3


1  

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

#1


4  

<%= 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将在路径的开始自动添加资产

check Image Tag for Documentation

检查图像标签的文档

#2


1  

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属性,您可以考虑使用选项散列,这将允许您将CSS属性作为键传递给您所需的值。

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

image_tag(“#{@random_image}”,高度:20,宽度:20)您可以查看前面答案中的文档

#3


1  

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