geniuskid kanyi geniuskid kanyi - 4 months ago 28
CSS Question

how to add inline css to rails link_to helper

am on rails 5 and my categories have images. i want to use those images as backround images but when i set in the styling the url do not change

<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

Answer

You need to interpolate the value of 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 %>