12

I recently rewrote one of my Rails 3.2.13 applications to use Rails 4. I used twitter-bootstrap-rails 2.1.2 in my Rails 3 application. When I rewrote the Rails 4 version it started using version 2.2.8. I'm using Phusion Passenger 4.0.20 on my development and production servers to host all my Rails applications.

In my Rails 3 application the background image appeared correctly in localhost and in production. However when I rewrote the application using Rails 4 and the current version of twitter-bootstrap-rails I noticed that the background image appears correctly but when I deploy it in production the background image does not display.

I have tried the following CSS but the background image only appears in localhost. The first statement is the original CSS code that was working well in my Rails 3 application in localhost and production.

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;

Here is the class that includes the background statement that originally worked:

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

Here is the code that I am using in my view:

<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>

I have cleared the cache in my browser for both localhost and my domain. I still see the image in localhost but not with the domain. I have tried using the latest version of Safari, Chrome, Firefox and Opera on my Mac Mini Server. All of them get the same results.

I could understand this if I was not seeing the background image in localhost. I did my testing for the Rails 4 app using localhost and assumed that it would work in production. However after I deployed it the background image was missing. This is happening on both my production and development servers. The image file is in app/assets/images on both servers.

My original code and all the examples I posted here are correct since the image displays in localhost. I have no clue where to check at this point. I may post this in the Passenger Google Group. That is the only other variable between localhost and production, especially considering my development server.

UPDATE: I decided to try other possibilities after I posted this. NOW the background image no longer appears for localhost. I'm completely frustrated with this.

Any help would be appreciated.

UPDATE 10/19/2013 8:05 PM CDT GMT-5

I attempted to implement the asset_url solution in my .less file. Here are the errors I got when I did the rake assets:precompile RAILS_ENV=production command

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)
  • 1
    If you remove the ../assets and replace with /assets as you originally had, restart your server (just in case you're caching) and dump the cache in your browser, does the image load again in development? – trh Oct 19 '13 at 02:18
  • I reset the browser, cleared cache and rebooted my Mac Mini Server. The image does not appear. Maybe I had cache for all these days when I was running localhost. Now I just do not see the background image at all at this point. – Pamela Cook - LightBe Corp Oct 19 '13 at 03:06
  • 1
    Are you compiling the assets with digest on? If yes it may not work cause theres an issue with `sprocket-rails` https://github.com/rails/sprockets-rails/issues/49 You may try - https://github.com/alexspeller/non-stupid-digest-assets – swapab Oct 19 '13 at 08:00
  • Where is your production environment? Heroku or other? – Richard Peck Oct 19 '13 at 09:36
  • Rich, my production environment is using Phusion Passenger on a Mac Mini Server (OS X Server - Mountain Lion) in another location. My development environment is also a Mac Mini Server in our home office (OS X Server - Mountain Lion). The problem happens when I point my applications to a domain on both servers. – Pamela Cook - LightBe Corp Oct 19 '13 at 11:56
  • swap.nil, I will check out your links later today. I have an appointment in a couple of hours. Something weird is going on. When I display http://localhost:3000/assets/landingpage2.jpg I can see the image. However when I try to directly access the image using my production domain I get the Rails default page not found error. – Pamela Cook - LightBe Corp Oct 19 '13 at 12:12

2 Answers2

26

Your problem with almost certainly be to do with rake assets:precompile & your production environment

A good test will be to Right-Click > View Source on your production app and click on the CSS file where your background image is called. If your problem is your CSS is calling url("background-image.png") & when you click onto the file, it doesn't show, the solution is to use SCSS to precompile the assets dynamically


Precompiling The Assets With SCSS

We had the problem where you couldn't access the image files in the CSS, and found it was because the CSS was only referencing static url() locations; and in precompile, our app was calling images "backgroun-image-234234nsdfasfdjasdfk2jij234ij32i.png"

The way to fix that is to use SCSS to create dynamic links to the assets; allowing your app to put the correct path to the images, etc. Here is some live code:

#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')
}

If you do this, and then when you push to production, you perform these commands, it should work:

rake assets:precompile RAILS_ENV=production
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • With using the gem all my css code is in bootstrap_and_overrides.css.less. I'm not using application.css. Could I implement what you are mentioning just for the section where I am referencing the background image and leave the rest of the code in bootstrap_and_overrides.css.less where I have a lot of LESS code? – Pamela Cook - LightBe Corp Oct 19 '13 at 12:22
  • .less is supported by the Rails "sprockets" compiler (http://stackoverflow.com/questions/7341676/using-less-in-a-rails-3-1-app), which means you'll get the same results from applying the "asset_url" stuff to your .less files – Richard Peck Oct 19 '13 at 12:25
  • I attempted the asset_url solution in my .less file. When I did rake assets:precompile without the environment the command executed without errors. When I did rake assets:precompile RAILS_ENV=production (first time I have included the environment) posted a 'Writing' statement for all the images in assets/images the process aborted. I added the error log starting with the last 'Writing' statement. – Pamela Cook - LightBe Corp Oct 20 '13 at 01:16
  • I saw my typing error and tried it again. My background image is displaying properly!!!! – Pamela Cook - LightBe Corp Oct 20 '13 at 01:30
  • I spoke too soon. One of my background images displayed but now it is not displaying. I will reboot my server and see if they show up. Sigh.... – Pamela Cook - LightBe Corp Oct 20 '13 at 01:34
  • Just rebooted my server and it is not displaying. Since one of them showed up briefly it looks like the code should work. I will try and redo the images to see if they somehow got corrupted. Maybe they will show up afterwards. – Pamela Cook - LightBe Corp Oct 20 '13 at 01:40
  • After trying several options, including these mentioned, I'm back where I began. Now my background images again are displaying in localhost but not in production. At least I know that the images are not corrupted. Thanks for the input. – Pamela Cook - LightBe Corp Oct 21 '13 at 18:06
  • Want to go into chat? – Richard Peck Oct 21 '13 at 18:08
  • No problem. Thanks for the offer. Invite me. I have not done a chat here before :) – Pamela Cook - LightBe Corp Oct 21 '13 at 18:10
  • Let me make a chat room for us – Richard Peck Oct 21 '13 at 18:10
  • After the chat I was able to implement this successfully. I will push this to production on our production server. Thanks Rich for all your help. – Pamela Cook - LightBe Corp Oct 21 '13 at 18:34
  • No problem - glad I could help! You might want to add an upvote to the answer to make sure people know it's the right one (sometimes on SO, answers are approved, but ones are submitted later which are more appropriate & therefore receive higher votes) :) – Richard Peck Oct 21 '13 at 18:42
10

Fwiw, I just changed the name of the css file that referenced the background image from styleblah.css to styleblah.css.scss then changed background: url('/assets/images/imagetitle.png'); to background: asset_url('imagetitle.png'); and it worked perfectly.

Like the man says, run something along these lines: rake assets:precompile RAILS_ENV=production

then restart your web/http server (in my case unicorn) for good measure: sudo service unicorn restart

user2398041
  • 563
  • 1
  • 5
  • 9
  • having same issue, can you please put your prodction.rb file. Is config.assets.compile true ? – Shamith c Jan 29 '14 at 19:24
  • 1
    Hi sorry to hear about it–I didn't change the default production.rb configuration for my rails 4 app (aside from setting up mandrill); config.assets.compile is set to false: `config.assets.compile = false`. You can find my full production.rb file here: [Production.rb on Github](https://github.com/ryparty/productionrb/blob/master/production.rb). Are you making sure to change the path in your .css.scss document to just the name of the image and also where are you placing your image? I'm learning as well, but I don't see why it wouldn't work. – user2398041 Jan 30 '14 at 21:38