CSS未在应用中加载

Jam*_*mie 32 css assets ruby-on-rails ruby-on-rails-3 asset-pipeline

CSS不会加载到我的rails应用程序中.这是位于view/products中的index.html.erb文件:

 <h1>Listing products</h1>

<table>
<% @products.each do |product| %>
  <tr class="<%= cycle('list_line_odd', 'list_line_even') %>">

  <td>
    <%= image_tag(product.image_url, :class=> 'list_image') %>
  </td>

  <td class="list_description"> 
    <dl>
      <dt><%= product.title %></dt>
      <dd><%= truncate(strip_tags(product.description), :length=> 80) %></dd>
    </dl>
  </td>

  <td class="list_actions">
    <%= link_to 'Show', product %><br/>
    <%= link_to 'Edit', edit_product_path(product) %><br/>
    <%= link_to 'Destroy', product, 
              :confirm=> 'Are you sure?',
              :method=> :delete %>
  </td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New product', new_product_path %>
Run Code Online (Sandbox Code Playgroud)

然后我将application.html.erb文件放在view/layouts中.此文件应将css链接到html.

<!DOCTYPE html> 
<html>
<head>
  <title>Depot</title>
  <%= stylesheet_link_tag  "application" %>
  <%= javascript_include_tag  "application" %>
  <%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我的css文件products.css.scss位于assets/stylesheets中,如下所示:

.products {
  table {
    border-collapse: collapse;
  }

  table tr td {
    padding: 5px;
    vertical-align: top;
  }

 .list_image {
    width:  60px;
    height: 70px;
  }

  .list_description {
    width: 60%;

    dl {
      margin: 0;
    }

    dt {
      color:        #244;
      font-weight:  bold;
      font-size:    larger;
    }

    dd {
      margin: 0;
    }
  }

  .list_actions {
     font-size:    x-small;
     text-align:   right;
     padding-left: 1em;
  }

  .list_line_even {
    background-color:   #e0f8f8;
  }

  .list_line_odd {
    background-color:   #f8b0f8;
  }
}
Run Code Online (Sandbox Code Playgroud)

最后我的application.css文件如下所示:

/*
 * This is a manifest file that'll automatically include all the stylesheets available in this directory
 * and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
 * the top of the compiled file, but it's generally better to create a new file per style scope.
 *= require_self
 *= require_tree . 
 */
Run Code Online (Sandbox Code Playgroud)

一切看起来都没问题,因为我理解application.css收集所有其他的CSS文件,所以你不必手动链接它们.我对么?

此处还有加载页面时的服务器日志:

Started GET "/products" for 127.0.0.1 at Wed Dec 07 20:53:10 +0000 2011
  Processing by ProductsController#index as HTML
  Product Load (0.1ms)  SELECT "products".* FROM "products" 
 Rendered products/index.html.erb within layouts/application (7.4ms)
 Completed 200 OK in 26ms (Views: 24.2ms | ActiveRecord: 0.4ms)


 Started GET "/assets/scaffolds.css?body=1" for 127.0.0.1 at Wed Dec 07 20:53:10 +0000 2011
 Served asset /scaffolds.css - 304 Not Modified (0ms)


 Started GET "/assets/all.css" for 127.0.0.1 at Wed Dec 07 20:53:10 +0000 2011
 Served asset /all.css - 404 Not Found (4ms)

 ActionController::RoutingError (No route matches [GET] "/assets/all.css"):


 Rendered /Library/Ruby/Gems/1.8/gems/actionpack-                           3.1.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.5ms)


 Started GET "/assets/products.css?body=1" for 127.0.0.1 at Wed Dec 07 20:53:10 +0000 2011
 Served asset /products.css - 304 Not Modified (0ms)


 Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at Wed Dec 07 20:53:10 +0000 2011
 Served asset /jquery.js - 304 Not Modified (0ms)


 Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at Wed Dec 07 20:53:10 +0000 2011
 Served asset /jquery_ujs.js - 304 Not Modified (0ms)


 Started GET "/assets/products.js?body=1" for 127.0.0.1 at Wed Dec 07 20:53:10 +0000 2011
 Served asset /products.js - 304 Not Modified (0ms)


 Started GET "/assets/defaults.js" for 127.0.0.1 at Wed Dec 07 20:53:10 +0000 2011
 Served asset /defaults.js - 404 Not Found (3ms)

 ActionController::RoutingError (No route matches [GET] "/assets/defaults.js"):


 Rendered /Library/Ruby/Gems/1.8/gems/actionpack-          3.1.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.9ms)
Run Code Online (Sandbox Code Playgroud)

为什么我的应用没有显示任何CSS?

Sub*_*ial 24

你有没有这个ProductsController,或者如果没有,ApplicationController

layout "application"
Run Code Online (Sandbox Code Playgroud)

你有一个预先资产管道<%= stylesheet_link_tag :all %>,通常包括所有样式表/public/stylesheets,但它告诉资产管道寻找一个名为的文件all.css.

由于<%= stylesheet_link_tag :all %>显然不在您的application.html.erb布局中,因此正在呈现其他一些布局.寻找它,和/或确保您的控制器调用正确的布局.


Mic*_*ant 9

运行rake assets:precompile以编译并将您的css从/ assets复制到/ public(rails 3.1+)


rub*_*ect 7

我的情况是在我的样式表中修复错误并运行:

$ RAILS_ENV=development rake assets:clean
Run Code Online (Sandbox Code Playgroud)

解决了这个问题.

运行以上命令会删除生成的CSS文件,并强制rails重新生成Sass或SCSS文件的CSS输出.


小智 2

好吧,修改 app/views/layouts/application.html.erb 可能会起作用,将“stylesheet_link_tag“application””更改为“stylesheet_link_tag“depot””。

我希望它能帮助你!