在 Rails 5 模板中为内联背景图像 url 添加 image_tag

Adding an image_tag for the inline background-image url in Rails 5 template

我需要使用样式标签内联设置背景图像,但它需要引用一个图像对象,而不是资产文件夹中的固定图像。下面是我的代码来展示我正在尝试做什么。

<div class="background-image-holder" style="background: url(#{" image_path @product.photo_one_url(:original)"}); opacity: 1;">
  <%= image_tag(@product.photo_one_url(:medium)) %>
</div>

如果我包含像这样的动态内容,我总是更喜欢使用 content_tag,即

<%= content_tag :div, class: "background-image-holder", 
                      style: { background: "url('#{image_path(@product.photo_one_url(:original))}')", 
                               opacity: 1 } do %>
...
<% end >

我觉得它比使用内联 erb 标签更好读 - 个人喜好,但对您来说是另一种选择。