【问题标题】:precompile problems rails assets预编译问题 rails assets
【发布时间】:2011-10-06 19:19:10
【问题描述】:

我确定我只是以错误的方式执行此操作,但我无法让它与 assets:precompile 一起使用,我不确定它应该是均匀的。

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

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

【问题讨论】:

    标签: ruby-on-rails-3.1 precompile asset-pipeline


    【解决方案1】:

    我不确定您的语法有什么问题,但整个方法似乎与规范有点颠倒。通常不会为每个对象创建新的 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;}
    

    也许您不这样做是因为不同颜色的植物数量有限?例如。你有一整个彩虹的颜色吗?在那种情况下,真的更适合“老派”并使用样式标签:

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

    【讨论】:

    • 我接受了你的风格建议,因为颜色是由用户动态选择的。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    • 2021-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    相关资源
    最近更新 更多