如何将内联 css 添加到 rails link_to 助手
how to add inline css to rails link_to helper
我在 rails 5,我的类别有图片。我想将这些图像用作背景图像,但是当我在样式中设置时 url 不会更改
<div class="grid-category">
<% @servicescategories.each do |category| %>
<%= link_to servicecategories_path(slug: category.slug ), :style=>'background-image: asset-data-url("category.category_image");', class: "category-item" do %>
<h3> <%= category.name %></h3>
<% end %>
<% end %>
</div>
我哪里做错了
您需要对 category.category_image
的值进行插值
<%= link_to servicecategories_path(slug: category.slug ),
class: "category-item" do %>
<div style="background-image: url(<%= asset_path('category.category_image') %>)">
<h3> <%= category.name %></h3>
</div>
<% end %>
您的语法似乎不正确,无法在 style 属性中使用 rails 插值,
试试这个,
<%= link_to servicecategories_path(slug: category.slug ),
:style=>"background-image: <%= asset-data-url(category.category_image) %>",
class: "category-item" do %>
这个有效
<%= link_to servicecategories_path(slug: category.slug ),
class: "category-item" do %>
<div style="background-image: <%= asset_path('category.category_image') %>">
<h3> <%= category.name %></h3>
</div>
<% end %>
我在 rails 5,我的类别有图片。我想将这些图像用作背景图像,但是当我在样式中设置时 url 不会更改
<div class="grid-category">
<% @servicescategories.each do |category| %>
<%= link_to servicecategories_path(slug: category.slug ), :style=>'background-image: asset-data-url("category.category_image");', class: "category-item" do %>
<h3> <%= category.name %></h3>
<% end %>
<% end %>
</div>
我哪里做错了
您需要对 category.category_image
<%= link_to servicecategories_path(slug: category.slug ),
class: "category-item" do %>
<div style="background-image: url(<%= asset_path('category.category_image') %>)">
<h3> <%= category.name %></h3>
</div>
<% end %>
您的语法似乎不正确,无法在 style 属性中使用 rails 插值, 试试这个,
<%= link_to servicecategories_path(slug: category.slug ),
:style=>"background-image: <%= asset-data-url(category.category_image) %>",
class: "category-item" do %>
这个有效
<%= link_to servicecategories_path(slug: category.slug ),
class: "category-item" do %>
<div style="background-image: <%= asset_path('category.category_image') %>">
<h3> <%= category.name %></h3>
</div>
<% end %>