如何将内联css添加到rails link_to helper

时间:2016-07-28 10:04:15

标签: html css ruby-on-rails ruby-on-rails-5

am on rails 5,我的类别有图片。我想将这些图像用作背景图像,但是当我在样式中设置时,网址不会改变

<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>

我在这里做错了什么

3 个答案:

答案 0 :(得分:0)

您需要插入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 %>

答案 1 :(得分:0)

您似乎使用不正确的语法在style属性中使用rails插值, 试试这个,

 <%= link_to servicecategories_path(slug: category.slug ),
 :style=>"background-image: <%= asset-data-url(category.category_image) %>",
   class: "category-item" do %>

答案 2 :(得分:0)

这是有效的

<%= 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 %>