ruby-on-rails-3 – Rails CSS加载,但不应用样式
我正在关注
http://pragprog.com/book/rails4/agile-web-development-with-rails这本书,我的scss文件无效.
css文件是这样的: .store { h1 { margin: 0; padding-bottom: 0.5em; font: 150% sans-serif; color: #226; border-bottom: 3px dotted #77d; } /* An entry in the store catalog */ .entry { overflow: auto; margin-top: 1em; border-bottom: 1px dotted #77d; min-height: 100px; img { width: 80px; margin-right: 5px; margin-bottom: 5px; position: absolute; } h3 { font-size: 120%; font-family: sans-serif; margin-left: 100px; margin-top: 0; margin-bottom: 2px; color: #227; } p,div.price_line { margin-left: 100px; margin-top: 0.5em; margin-bottom: 0.8em; } .price { color: #44a; font-weight: bold; margin-right: 3em; } } } 和html文件如下: <% if notice %> <p id="notice"><%= notice %></p> <% end %> <h1>Your Pragmatic Catalog</h1> <% @products.each do |product| %> <div class="entry"> <%= image_tag(product.image_url) %> <h3><%= product.title %></h3> <p><%= sanitize(product.description) %></p> <div class="price_line"> <span class="price"><%= product.price %></span> </div> </div> <% end %> CSS正在正确加载,但未应用.但是,如果添加一个带有“store”类的周围div,它就可以工作.这本书没有提到这种情况,我相信应该“自动”应用这种风格吧? 谢谢. **编辑******** 我发现了这个问题.对于可能遇到相同问题的人,请检查文件: 应用程序/资产/视图/布局/ application.html.erb body标签应该包含以下代码: <body class="<%= controller.controller_name %>"> 解决方法
很高兴您找到了解决方案.但我试图解释幕后发生的事情.
你使用CSS的方式不是一般惯例.这个设施附带一些额外的宝石.检查此链接https://stackoverflow.com/a/4564922/1160106.使用这些宝石,您可以设计您的css更多DRY方式. 一般公约 如果要将样式应用于以下h1元素 # Here "store" class is the parent element of "h1" <div class="store"> <h1> some text </h1> </div> 将需要以下css方式 #Here also "store" is written before "h1" .store h1 { #some designs } 你的情况怎么样? 可能你正在维护控制器智能css文件.并假设你有一个stores_controller.这就是为什么store_controller的类被封装在.store {}块中的原因.喜欢 .store { h3 {font-size: 120%;} } 很明显,你的h3元素需要一个具有商店类的父元素.而你这样做是通过添加class =“<%= controller.controller_name%>”来实现的.用你的身体标签.毫无疑问,< body> tag是以下所有节点的父级.现在,当您点击stores_controller时,它会设置class =“store”并且您的样式正在运行. 这种方法确实是DRY并且值得推荐. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |