简体   繁体   English

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

[英]how to add inline css to rails link_to helper

am on rails 5 and my categories have images. 在5栏上,我的类别中有图片。 i want to use those images as backround images but when i set in the styling the url do not change 我想将这些图像用作背景图像,但是当我设置样式时,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>

what am i doing wrong here 我在这里做错了什么

You need to interpolate the value of category.category_image 您需要插值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 %>

You seems to have the incorrect syntax to use the rails interpolation inside the style attribute, Try this, 您似乎在style属性中使用rails插值的语法不正确,请尝试以下操作,

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

this works 这有效

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM