12

我最近重写了我的 Rails 3.2.13 应用程序之一以使用 Rails 4。我在 Rails 3 应用程序中使用了 twitter-bootstrap-rails 2.1.2。当我重写 Rails 4 版本时,它开始使用 2.2.8 版本。我在我的开发和生产服务器上使用 Phusion Passenger 4.0.20 来托管我所有的 Rails 应用程序。

在我的 Rails 3 应用程序中,背景图像在 localhost 和生产环境中正确显示。但是,当我使用 Rails 4 和当前版本的 twitter-bootstrap-rails 重写应用程序时,我注意到背景图像正确显示,但是当我在生产中部署它时,背景图像不显示。

我尝试了以下 CSS,但背景图像仅出现在 localhost 中。第一条语句是原始 CSS 代码,它在我的 Rails 3 应用程序中运行良好,在本地主机和生产环境中运行良好。

background: url(/assets/landingpage2.jpg); width: 574px; height: 650px;

background: url(../assets/landingpage2.jpg); width: 574px; height: 650px;

background-image: url(../assets/landingpage2.jpg); width: 574px; height: 650px;

background-image: url("../assets/landingpage2.jpg"); width: 574px; height: 650px;

background-image: url("/assets/landingpage2.jpg"); width: 574px; height: 650px;

这是包含最初工作的背景语句的类:

.landing-cube-menu {
  font-weight: bold; 
  font-size: 150%;
  line-height: 100%;    
  background: url(/assets/landingpage2.jpg); width: 574px; height: 650px;
  margin:0 auto;  
}

这是我在视图中使用的代码:

<div class="header-cube-menu" style="position: relative;">

  <div class="header-store" style="position: absolute;">
    <p><%= link_to "Online Store", store_path %></p>
  </div>

  <div class="header-about" style="position: absolute;">
    <p><%= link_to "About", about_path %></p>
  </div>

  <div class="header-contact" style="position: absolute;">
    <p><%= link_to "Contact", contact_path %></p>
  </div>

  <div class="header-verse" style="position: absolute;">
    <p><a href="/verse">Bible<br>Verse</a></p>
  </div>

  <div class="header-app" style="position: absolute;">
    <p><a href="/app">Mobile<br>&nbsp;&nbsp;App</a></p>
  </div>

  <div class="header-home" style="position: absolute;">
    <p><a href="/home">Home<br>&nbsp;Page</a></p>
  </div>

  <div class="header-math" style="position: absolute;">
    <p><a href="/math">The<br>Math</a></p>
  </div>

  <div class="header-vision" style="position: absolute;">
    <p><a href="/vision">&nbsp;Vision<br>Mission</a></p>
  </div>

  <div class="header-pendant" style="position: absolute;">
    <p><a href="/pendant">Pendant<br>&nbsp;&nbsp;&nbsp;Desk Cross<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;and more</a></p>
  </div>

  <div class="header-puzzle" style="position: absolute;">
    <p><a href="/puzzle">3D-Puzzle<br>&nbsp;&nbsp;eBook</a></p>
  </div>

</div>

我已经清除了浏览器中本地主机和域的缓存。我仍然在 localhost 中看到图像,但在域中看不到。我尝试在我的 Mac Mini 服务器上使用最新版本的 Safari、Chrome、Firefox 和 Opera。他们都得到相同的结果。

如果我没有在 localhost 中看到背景图像,我可以理解这一点。我使用 localhost 对 Rails 4 应用程序进行了测试,并假设它可以在生产环境中工作。但是,在我部署它之后,背景图像丢失了。这发生在我的生产和开发服务器上。图像文件位于两台服务器上的 app/assets/images 中。

我的原始代码和我在此处发布的所有示例都是正确的,因为图像显示在 localhost 中。我现在不知道在哪里检查。我可以在乘客谷歌组中发布这个。这是 localhost 和生产之间的唯一其他变量,特别是考虑到我的开发服务器。

更新:发布此消息后,我决定尝试其他可能性。现在背景图像不再出现在本地主机上。我对此感到非常沮丧。

任何帮助,将不胜感激。

更新 2013 年 10 月 19 日晚上 8:05 CDT GMT-5

我试图在我的 .less 文件中实现asset_url解决方案。这是我在执行rake assets 时遇到的错误:precompile RAILS_ENV=production命令

I, [2013-10-19T19:59:05.384878 #69440]  INFO -- : Writing /Users/myusername/Sites/myrailsapp/public/assets/application-f8821f5601f793a09e9373fb397df1b1.js
    rake aborted!
    undefined method `[]' for nil:NilClass
      (in /Users/myusername/Sites/hypercubeoflove_RoR_4/app/assets/stylesheets/application.css)
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sprockets-2.10.0/lib/sprockets/sass_functions.rb:63:in `sprockets_context'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-rails-4.0.0/lib/sass/rails/helpers.rb:23:in `asset_url'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/script/funcall.rb:112:in `_perform'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/script/node.rb:40:in `perform'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/tree/visitors/perform.rb:298:in `visit_prop'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/tree/visitors/base.rb:37:in `visit'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/tree/visitors/perform.rb:100:in `visit'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/tree/visitors/base.rb:53:in `block in visit_children'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/tree/visitors/base.rb:53:in `map'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/tree/visitors/base.rb:53:in `visit_children'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/tree/visitors/perform.rb:109:in `block in visit_children'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/tree/visitors/perform.rb:121:in `with_environment'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/tree/visitors/perform.rb:108:in `visit_children'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/tree/visitors/base.rb:37:in `block in visit'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/tree/visitors/perform.rb:320:in `visit_rule'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/tree/visitors/base.rb:37:in `visit'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/tree/visitors/perform.rb:100:in `visit'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/tree/visitors/base.rb:53:in `block in visit_children'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/tree/visitors/base.rb:53:in `map'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/tree/visitors/base.rb:53:in `visit_children'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/tree/visitors/perform.rb:109:in `block in visit_children'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/tree/visitors/perform.rb:121:in `with_environment'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/tree/visitors/perform.rb:108:in `visit_children'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/tree/visitors/base.rb:37:in `block in visit'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/tree/visitors/perform.rb:128:in `visit_root'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/tree/visitors/base.rb:37:in `visit'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/tree/visitors/perform.rb:100:in `visit'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/tree/visitors/perform.rb:7:in `visit'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/tree/root_node.rb:20:in `render'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/engine.rb:315:in `_render'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sass-3.2.12/lib/sass/engine.rb:262:in `render'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sprockets-2.10.0/lib/sprockets/sass_compressor.rb:24:in `evaluate'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/tilt-1.4.1/lib/tilt/template.rb:103:in `render'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sprockets-2.10.0/lib/sprockets/context.rb:197:in `block in evaluate'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sprockets-2.10.0/lib/sprockets/context.rb:194:in `each'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sprockets-2.10.0/lib/sprockets/context.rb:194:in `evaluate'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sprockets-2.10.0/lib/sprockets/bundled_asset.rb:25:in `initialize'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sprockets-2.10.0/lib/sprockets/base.rb:377:in `new'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sprockets-2.10.0/lib/sprockets/base.rb:377:in `build_asset'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sprockets-2.10.0/lib/sprockets/index.rb:94:in `block in build_asset'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sprockets-2.10.0/lib/sprockets/caching.rb:58:in `cache_asset'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sprockets-2.10.0/lib/sprockets/index.rb:93:in `build_asset'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sprockets-2.10.0/lib/sprockets/base.rb:287:in `find_asset'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sprockets-2.10.0/lib/sprockets/index.rb:61:in `find_asset'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sprockets-2.10.0/lib/sprockets/manifest.rb:211:in `block in find_asset'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sprockets-2.10.0/lib/sprockets/manifest.rb:257:in `benchmark'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sprockets-2.10.0/lib/sprockets/manifest.rb:210:in `find_asset'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sprockets-2.10.0/lib/sprockets/manifest.rb:119:in `block in compile'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sprockets-2.10.0/lib/sprockets/manifest.rb:118:in `each'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sprockets-2.10.0/lib/sprockets/manifest.rb:118:in `compile'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sprockets-rails-2.0.0/lib/sprockets/rails/task.rb:60:in `block (3 levels) in define'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sprockets-2.10.0/lib/rake/sprocketstask.rb:146:in `with_logger'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/gems/sprockets-rails-2.0.0/lib/sprockets/rails/task.rb:59:in `block (2 levels) in define'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/bin/ruby_noexec_wrapper:14:in `eval'
    /Users/myusername/.rvm/gems/ruby-2.0.0-p247@mygemset/bin/ruby_noexec_wrapper:14:in `<main>'
    Tasks: TOP => assets:precompile
    (See full trace by running task with --trace)
4

2 回答 2

26

您的问题几乎肯定rake assets:precompile与您的生产环境有关

一个好的测试是Right-Click > View Source在您的生产应用程序上单击调用背景图像的 CSS 文件。如果您的问题是您的 CSS 正在调用 url("background-image.png") 并且当您单击文件时,它不会显示,解决方案是使用 SCSS 动态预编译资产


使用 SCSS 预编译资产

我们遇到了无法访问 CSS 中的图像文件的问题,发现这是因为 CSS 仅引用静态 url() 位置;在预编译中,我们的应用程序调用图像“backgroun-image-234234nsdfasfdjasdfk2jij234ij32i.png”

解决这个问题的方法是使用 SCSS创建到资产的动态链接;允许您的应用程序将正确的路径放置到图像等。这是一些实时代码:

#application.css.scss (yes, you need to rename it)
@import 'layout/body'


#app/assets/stylesheets/layout/body.css.scss
body {
    background: asset_url('background-image.png')
}

如果你这样做,然后当你推送到生产环境时,你执行这些命令,它应该可以工作:

rake assets:precompile RAILS_ENV=production
于 2013-10-19T09:46:50.703 回答
10

Fwiw,我只是将引用背景图像的 css 文件的名称从更改styleblah.cssstyleblah.css.scss然后更改background: url('/assets/images/imagetitle.png');background: asset_url('imagetitle.png');,它工作得很好。

就像那个人说的那样,按照以下方式运行:rake assets:precompile RAILS_ENV=production

然后重新启动您的 web/http 服务器(在我的情况下是独角兽)以进行良好的测量:sudo service unicorn restart

于 2014-01-17T08:59:26.383 回答