如何將轉換為rails應用中的image_tag ?

[英]How to convert 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_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將理解您的代碼,希望這有助於澄清一些……


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/09/28/72ffa8c198ee427257d0084a4f68867.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com