这是我在这里的第一篇文章,它可能听起来很愚蠢。我建立我的第一个rails应用程序。如何在0123应用程序中将<img src= >转换为image_tag
我在index.html.erb
<img src="/assets/rand_front/<%= @random_image%>", style='height:50vw;width:100vw;margin-bottom:20px;' >
此行中我想用image_tag
代替img src
什么是它环绕代码的正确方法是什么?
到目前为止,我已经试过<%= 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;' %>
和其他许多版本,但没有一个似乎工作,我究竟做错了什么?我该如何正确书写它?
这个<%= @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
BTW,您可以用'@random_image =“#{兰特(1..10)}。jpg“'或者 - 如果这些只是占位符 - ”@random_image = @ images.sample' – Stefan