Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 Rails CSS加载,但样式不是';不适用_Ruby On Rails 3_Sass - Fatal编程技术网

Ruby on rails 3 Rails CSS加载,但样式不是';不适用

Ruby on rails 3 Rails CSS加载,但样式不是';不适用,ruby-on-rails-3,sass,Ruby On Rails 3,Sass,我正在阅读这本书,我的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;

我正在阅读这本书,我的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,它就会工作。这本书没有提到这种情况,我相信它应该“自动”应用这种风格,对吗

谢谢

***编辑*********

我发现了问题。对于可能遇到相同问题的人,请检查文件:

app/assets/views/layouts/application.html.erb

正文标签应具有以下代码:

<body class="<%= controller.controller_name %>">

根据您的代码,所有样式都在
.store{}
块之间,因此只要您使用类“store”包围div,它就不会反映

比如说

.store {
  h3 {
      font-size: 120%;
      font-family: sans-serif;
      margin-left: 100px;
    }
}

.store h3 {
      font-size: 120%;
      font-family: sans-serif;
      margin-left: 100px;
      margin-top: 0;
      margin-bottom: 2px;
      color: #227;
    }

很好,你找到了解决办法。但我想解释一下幕后发生了什么

您使用css的方式不是一般惯例。这个设施附带了一些额外的宝石。检查此链接。有了这些宝石,你可以用更多的方式设计你的css


大会 如果要将样式应用于以下
h1
元素

# Here "store" class is the parent element of "h1"
<div class="store">
     <h1> some text </h1>
</div>

你的情况怎么样了? 可能您正在维护控制器明智的css文件。假设你有一个
存储控制器
。这就是为什么
stores\u控制器的类被封装在
.store{}
块中的原因。像

.store {
    h3 {font-size: 120%;}
}
因此很明显,您的
h3
元素需要一个具有
store
类的父元素。您可以通过在
body
标签中添加
class=“
来实现。毫无疑问,
标记是以下所有节点的父节点。现在,当您点击
stores\u controller
时,它将设置
class=“store”
,并且您的样式正在工作


这种方法确实是值得推荐的。

是的,就像我说的,如果我用store类添加一个周围的div,这种样式就可以工作了。然而,这本书从来没有提到过这一点!我相信没有这个应该行得通。谢谢你的解释。我知道这不是正常的css,而且幕后发生了什么,但是,由于我是Rails新手,我不知道它在Rails中是如何工作的。我只是不知道类名是否应该在某个地方。我怀疑Rails以某种方式将样式表“连接”到了这个特定的控制器视图。然而,当我找到解决方案时,我发现它比我预期的要简单得多:)
.store {
    h3 {font-size: 120%;}
}