无法加载样式表

时间:2016-08-03 03:46:24

标签: ruby-on-rails nginx unicorn digital-ocean

我似乎无法让我的rails项目看到我的style.css或style-responsive.css。

application.html.erb包含

 <!-- From ruby begin --> 
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>

 <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>

    <%= javascript_include_tag  "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" %>
  <%= javascript_include_tag  "http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js" %>
  <%= stylesheet_link_tag "style" %>                        <!-- From template   -->
  <%= stylesheet_link_tag "style.ie7" %>                    <!-- From template   -->
  <%= stylesheet_link_tag "style.responsive" %>             <!-- From template   -->
  <%= javascript_include_tag "script.js" %>                 <!-- From template   -->

  <% # javascript_include_tag 'defaults' %>


  <%= csrf_meta_tags %> <!--  cross-site request forgery protection parameter   -->

<!--[if lt IE 9]><script src="https://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
<link rel="stylesheet" href="style.css" media="screen">
<!--[if lte IE 7]><link rel="stylesheet" href="style.ie7.css" media="screen" /><![endif]-->
<link rel="stylesheet" href="style.responsive.css" media="all">

当我渲染页面时,文本显示但没有图形。我检查了服务器应用程序/文件夹和图像,css和js文件似乎在那里。

我做了一个rake资产:预编译。从我附上的图像看来,似乎有一些造型工作。

Assets.rb包含

 Rails.application. config.assets.precompile = ['*.js', '*.css', '*.css.erb']      #for Production
  Rails.application.config.assets.precompile += %w[*.png *.jpg *.jpeg *.gif]        #for Production
  Rails.application.config.assets.precompile += %w(script.js)
  Rails.application.config.assets.precompile += %w(script.responsive.js)
  Rails.application.config.assets.precompile += %w( style.css )
  Rails.application.config.assets.precompile += %w( style.responsive.css )
  Rails.application.config.assets.precompile += %w( style.ie7.css )

Production.rb已

  # Apache or NGINX already handles this.
 config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
  # config.serve_static_files = true

如果我查看我的生产日志,我会看到样式和样式的错误 - responsive.css

I, [2016-08-02T23:26:31.129752 #12930]  INFO -- : Started GET "/style.responsive.css" for 108.16.108.174 at 2016-08-02 23:26:31 -0400
I, [2016-08-02T23:26:31.133345 #12921]  INFO -- : Started GET "/style.css" for 108.16.108.174 at 2016-08-02 23:26:31 -0400
F, [2016-08-02T23:26:31.200006 #12921] FATAL -- : 
ActionController::RoutingError (No route matches [GET] "/style.css"):
  actionpack (4.2.5.2) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.2.5.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'

我认为数字海洋的ubuntu / rails将提供静态资产&#39;开箱即用&#39;

这将是一个低使用率的网站,所以我可以忍受只是获取铁路来提供静态资产(我试过config.serve_static_files = true并且似​​乎没有用)

我认为更好的解决方案是确保NGINX提供静态文件,但我不确定如何检查。

另外,如果我查看页面源代码,我会看到指纹文件。如果我点击其中一个,我会看到css文件。

 <!-- The following is added to connect the template to ruby --> 
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
  <link rel="stylesheet" media="screen" href="/assets/style-bf197dd0f531130964616a51129935da1e598a02a4875c978ba88a1150f8c83b.css" />                        <!-- From template   -->
  <link rel="stylesheet" media="screen" href="/assets/style.ie7-0f23e9b569741aa18fa91789e00826aaee0273a38bc82afb353167975630e750.css" />                    <!-- From template   -->
  <link rel="stylesheet" media="screen" href="/assets/style.responsive-8d9d7ff8e357d6e354b2c30b39e501b0911171f97f4618b2b6ae29d23ac20991.css" />             <!-- From template   -->
  <script src="/assets/script-f4f7e1cb97bf4be4d67e931693643a62b9c0fbd4250eb7e1a26a8c0cf555f4e3.js"></script>                 <!-- From template   -->
<!--  < %= javascript_include_tag "jquery.are-you-sure" %>    -->   <!-- Dirty form checker   -->

我认为服务器正在application.html.erb中查找以下样式表。但是,如果我删除了那段代码,我仍然无法获取我的图片,而且我也没有在日志中看到任何致命错误。我不确定下面的块是否必要。

<!--[if lt IE 9]><script src="https://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
    <link rel="stylesheet" href="style.css" media="screen">
    <!--[if lte IE 7]><link rel="stylesheet" href="style.ie7.css" media="screen" /><![endif]-->
    <link rel="stylesheet" href="style.responsive.css" media="all">

enter image description here

1 个答案:

答案 0 :(得分:1)

只需转到您的app / assets / stylesheets / application.css或.scss或.sass,然后将样式表从application.rb中删除

如果你有aplication.css

//=require style
//=require style.responsive

如果你有aplication.sass

@import 'style'
@import 'style.responsive'

如果你有aplication.scss

@import "style";
@import "style.responsive";

现在你的问题一定已经解决了