Rails 资产预编译问题

发布于 2024-12-08 07:33:01 字数 558 浏览 1 评论 0原文

我确信我只是以错误的方式这样做,但我无法让它与资产一起使用:预编译,我不确定它应该是均匀的。

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

我收到此错误:

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

我感谢任何人可以提供的任何帮助。如果我没有提供足够的信息,请告诉我我需要什么,我很乐意提供。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝對不後悔。 2024-12-15 07:33:01

我不确定你的语法有什么问题,但整个方法似乎有点偏离规范。通常,人们不会为每个对象创建新的 CSS 规则。为什么不在

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

你的 CSS 文件(不需要 ERB)中定义一些类,

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

也许你不这样做是因为不同颜色的植物数量不是有限的?例如,你有彩虹般的颜色吗?在这种情况下,“老派”并使用 style 标签确实更合适:

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

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 %>" >  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文