Вопрос

I am certain I am just doing this the wrong way, but I cant get this to work with assets:precompile and im not sure it should be even.

#plant.css.erb
<%
plants = Plant.all
if plants
  plants.each do |plant|
%>
    .plant_<%= plant.id %> {
        background-color: #<%= plant.color %>;
        padding: 1px;
    }
<%
  end
end
%>

I get this error:

Invalid CSS after "...kground-color: ": expected expression (e.g. 1px, bold), was "#;"
/rails/ship/releases/20111006191503/app/assets/stylesheets/application.css)

I appreciate any help that anyone can give. If I did not provide enough info, let me know what I need and I will gladly provide it.

Это было полезно?

Решение

I'm not sure what's wrong with your syntax, but the approach as a whole seems a bit upside down from the norm. Typically one would not create a new CSS rule for each object. Why not something like

# views/plants/index.html.erb
<div class="plant plant-<%= plant.color %>
...
</div>

and then in your CSS file (no need for ERB) define just a few classes

# plant.css
plant_red {background-color: #F00;}
plant_blue {background-color: #00F;}

Maybe you're not doing that because there are not a finite number of different colored plants? E.g. you have a whole rainbow of colors? In that case, it's really more appropriate to go "old school" and use the style tag:

# views/plants/index.html.erb
<div class="plant" style="background-color:#<%= plant.color %>" >  
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top