10

I have a Rails app that uses background images from a style.css.scss file. I have found multiple ways of having the images show up on localhost, but no ways to get them to display on Heroku.

I have looked at MANY posts on SO like this and this, as well as other sites like this, but nothing has worked so far.

Here is the code I have in my style.css.scss:

.hero-000 {
    width: 102%;
    background: url(asset-path("hero-000.jpg")) no-repeat center center fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}

However, I have also tried background-image, image-url, asset-url, and numerous other permutations as found in the linked SO posts.

I have this in my production.rb file:

  config.serve_static_files = true
  config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
  config.assets.compile = true
  config.assets.digest = true

And this in my application.html.erb file to call the css sheet:

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

As suggested by other posts, I have added this to my application.rb:

config.assets.initialize_on_precompile = false

Any ideas on how this can be resolved would be happily received!

ADDITIONAL INFO

Here's my gemfile:

source 'https://rubygems.org'

gem 'rails', '4.2.5'

group :production do
  gem 'pg'
  gem 'rails_12factor'
end

group :development do
  gem 'sqlite3'
  gem 'binding_of_caller'
  gem 'better_errors'
end

gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'bcrypt', '~> 3.1.7'
gem 'bootstrap-sass'
gem 'friendly_id', '~> 5.1.0'
gem 'google-analytics-rails', '1.1.0'
gem 'paperclip'
gem 'meta-tags'
gem 'bootsy'
gem 'devise'

Here are my console errors in localhost: enter image description here

And in Heroku: enter image description here

Community
  • 1
  • 1
Liz
  • 1,369
  • 2
  • 26
  • 61
  • Is the image in source control ? can you verify that the image is on heroku ? – xyious May 23 '16 at 23:22
  • How do you check that? – Liz May 23 '16 at 23:32
  • Do you have a public link to that page that you can post? Looking at the generated source might give a clue. – Ryenski May 24 '16 at 01:19
  • Is the image located in the public directory, or under `app/assets`? – Ryenski May 24 '16 at 01:20
  • My heroku link is https://pure-gorge-23608.herokuapp.com – Liz May 24 '16 at 01:22
  • And the photos are in app/assets/images. – Liz May 24 '16 at 01:24
  • Please add your Gemfile to the question so we can inspect it – Wes Foster May 26 '16 at 03:24
  • I edited the original post to include the gemfile. Let me know any other info that could be relevant. – Liz May 26 '16 at 03:27
  • Do you have this in a github repo? Looking at the source it is the only one not being MD5 hashed so it is not being delivered through the pipeline. Is this the only image being served from css? also it is the only one being served from /images where the pipeline ones come from /assets – myself Jun 01 '16 at 03:27

9 Answers9

2

Look at your paths.. It must be something with your configuration. You've got your absolute path in code somewhere, either that or it's on track to something related. Search your code-base and make sure you don't have your own machine's absolute path hard-coded anywhere. Make sure it is all relative. /Users/elizabethbayardelle/Dropbox/Code/BIA/ should not be anywhere in your source, but somewhere it is being used in your herokuapp instance. On your localhost screenshot you've even got a path to heroku.com... how? A second take over how you've configured things will fix this I'm sure.

Jonathan
  • 10,936
  • 8
  • 64
  • 79
  • 1
    Thanks for taking the time to look at this. (And for calling me not a complete amateur...some days it doesn't feel that way.) I definitely don't have anything hard-coded as everything was done using project paths. I tried out hard linking to heroku as a potential workaround, but it didn't work either. – Liz Jun 01 '16 at 22:11
  • You don't want to use any hard-coded paths at all though. What do you mean by project paths exactly? Have you tried forgoing path helper functions and just writing in the relative paths explicitly? If you are able to get it working on local, you can then see how things differ on the herokuapp instance (IIRC the only thing heroku takes control of is database instantiation). If your setup is in the same state and gives the same errors in console, you have a pathing issue somewhere, as your osx path has made it to the herokuapp instance – Jonathan Jun 01 '16 at 22:26
  • Yes. I tried both ways but got the same result. If you look at my "answer" down below I ended up just forgoing CSS-linked images all together in favor of erb `link_to` paths or html href links, both of which transferred over to Heroku just fine for some reason. – Liz Jun 01 '16 at 22:31
  • I just read in another one of your comments that you work on your source in a dropbox folder? Not sure how that works out but it's probably not a good idea. Do you use source control with git? You want to work on the project as if it were in a state to serve at all times. You may have troubled yourself with many red herrings having a working state for your dropbox environment but broken everywhere else. It's too difficult to second-guess without seeing the project but oh well. – Jonathan Jun 01 '16 at 22:37
1

If you want to use asset_path in your stylesheets, you'll need to convert them to ERB templates. Example: application.css.erb -- This makes the asset_path helper available within the template.

In most cases, however, the proper method to be used is image-url(). Just make sure that you use it in the correct manner:

background-image: image-url("hero-000.jpg");          # < Correct
background-image: url(image-url("hero-000.jpg"));     # < Incorrect
Wes Foster
  • 8,770
  • 5
  • 42
  • 62
  • With the `background-image: image-url("hero-000.jpg")` it doesn't even appear on my `localhost`. – Liz May 26 '16 at 02:39
  • What version of rails? Also, how is your style included in the template? Imported via css or included from the html? – Wes Foster May 26 '16 at 02:42
  • I'm using rails 4.2.5 and it is attached in `assets/stylesheets/style.css.scss`, but I've also tried (to no avail) to put it in the html. – Liz May 26 '16 at 02:44
  • Okay, I tried to convert my `style.css.scss` file to `style.css.scss.erb` because the Heroku help docs say that you have to have erb css sheets to use css assets with Rails 4. Now I'm getting an error that says `File to import not found or unreadable:` for my css.scss.erb sheet. I think we're on the right track, but I don't know how to get past this error... – Liz May 27 '16 at 16:41
  • Would this problem be due to the filename? Try `style.scss.erb` or `style.css.erb` and see if either of those two give a different (or no) error. Let me know – Wes Foster May 27 '16 at 18:04
  • I tried both of these permutations and neither changed the error. – Liz May 27 '16 at 18:24
1

Ok Liz i got the fix for this issue. just add the line to your config/environments/production.rb if run the server in production mode or add config/environments/development.rb if you run the server in development

config.BASE_URL = 'https://herokuapp.com/'

then

background: url(https://herokuapp.com/assets/hero-000.jpg) no-repeat center center fixed;

the image is not found as by default herokuapp create a subdomain for your app.

try this:

copy that image to public/img folder and then do following

background: url(https://pure-gorge-23608.herokuapp.com/img/hero-000.jpg) no-repeat center center fixed;
Pitabas Prathal
  • 1,006
  • 1
  • 12
  • 15
  • Unfortunately no luck. It still doesn't show up on `localhost` or on Heroku. I tried it both ways (in production and in development). – Liz May 26 '16 at 17:43
  • may be you did something worng just click the link you will get your image https://herokuapp.com/assets/stylesheet/hero-000.jpg – Pitabas Prathal May 26 '16 at 17:47
  • just reboot the server i think it's not get updated.can you inspect the page and send me the console errors – Pitabas Prathal May 26 '16 at 17:50
  • I tried restarting the server, but to no avail. I also tried with the `config.BASE_URL` changed in both production and development. I added my original post to include screenshots of the errors. – Liz May 26 '16 at 17:56
  • sry liz i have added the worng link one extra `stylesheets` added but now i updated it check it out – Pitabas Prathal May 26 '16 at 18:02
  • Tried the updated css. Still no dice on either platform. I get a 404 for the image on localhost and heroku. – Liz May 26 '16 at 18:05
  • https://herokuapp.com/assets/hero-000.jpg check this link and confirm me is this your image or not – Pitabas Prathal May 26 '16 at 18:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113065/discussion-between-liz-and-pitabas-prathal). – Liz May 26 '16 at 18:10
1

[EDIT]

Found this in the Rails 4 Asset Pipeline documentation (2.3.2). I know I posted a variation of this earlier, but maybe try each of these below and see what works?

For images specifically:

image-url("rails.png") becomes url(/assets/rails.png)

image-path("rails.png") becomes "/assets/rails.png"

The more generic form can also be used:

asset-url("rails.png") becomes url(/assets/rails.png)

asset-path("rails.png") becomes "/assets/rails.png"

socialpiranha
  • 182
  • 1
  • 1
  • 10
  • I tried putting the images in my public folder (precompiled) and it shows up in the localhost version, but not on heroku. – Liz May 28 '16 at 15:44
  • Would it be possible to remove the 'asset_path' and just use a basic relative path? I wonder if that's causing some issue - I don't understand why Heroku is even linking to your Dropbox folder? – socialpiranha May 29 '16 at 04:56
  • I work on different computers, so the whole app lives in my dropbox folder. And no, without the `asset_path` it doesn't appear on Heroku. With the asset_path (in the css.erb style sheet) it gives me a `file to import not found or unreadable` error. – Liz May 29 '16 at 05:05
  • Ah, that's probably the issue. Heroku will not have access to your Dropbox. One way you can work on multiple computers and still be updating the same application is using Github: https://github.com/ It's a version control software (that's free if you're okay with your app's code being public), where you can `fork` and `clone` your directory to get the most recent files on your computer, including the images. If there are many images, then it might be best to host your images on a separate server. (BTW, sorry if you already know about this and it doesn't solve your problem!!) – socialpiranha May 29 '16 at 05:14
  • But any images linked in my HTML are fine... It's just the CSS ones. Could that still be a Dropbox problem? – Liz May 29 '16 at 05:16
  • Can you show an example of how the images in your HTML are linked? – socialpiranha May 29 '16 at 05:19
  • This one comes across without issues on `localhost` and on Heroku: `<%= image_tag 'Flame.png', style: "height: 100%"%>` – Liz May 29 '16 at 05:21
  • Okay, so it seems like the format of how you linked to asset_path should be this since `asset_path` is an `ERB` specific helper : `.class { background-image: url(<%= asset_path 'image.png' %>) }` More info [here](http://stackoverflow.com/questions/8230725/css-in-rails-asset-path-not-processed-by-erb-in-development) – socialpiranha May 29 '16 at 05:27
  • Unfortunately, if I erb-ify my `css.scss` document I get a `file not found or unreadable` error. http://stackoverflow.com/questions/37488785/file-not-found-error-when-changing-style-css-scss-to-style-css-scss-erb/37492392?noredirect=1#comment62504166_37492392 – Liz May 29 '16 at 05:32
  • My bad! I didn't realize you were using `scss`. Try this: `background:url(asset_path('someImage.jpg', image))` (here's my [source](http://stackoverflow.com/questions/8334573/asset-path-in-scss-file-rails)) – socialpiranha May 29 '16 at 05:39
  • On localhost I get an `undefined method '[]' for "image":Sass::Script::Value::String` error. – Liz May 29 '16 at 05:43
  • I did try also `background-image: url(image_path('bg-hero-000.jpg')) no-repeat center center fixed;`, but to no avail. – Liz May 29 '16 at 05:47
  • Ugh, sorry to keep continuing this thread! I think it's supposed to be `asset-path` not `asset_path` – socialpiranha May 29 '16 at 05:48
  • Also, if this helps, when I try to look for the image via heroku using the web link (https://pure-gorge-23608.herokuapp.com/images/bg-hero-000.jpg) it says `The page you were looking for doesn't exist.`. I don't know what that means, but I feel like it's a clue... – Liz May 29 '16 at 06:00
  • Yep - so it seems like Heroku creates a unique fingerprint for each image that is linked with asset_path/image_path: (Ex. https://pure-gorge-23608.herokuapp.com/assets/card-blog-1b8892920581e7d201d992e13a1efebb53cc674563fc52ad2b6becbe8eb13865.jpg) Okay, one option is to remove `gem 'sass-rails' gem 'uglifier' gem 'coffee-rails'` from your Gemfile. It seems like Rails 4's asset pipeline automatically enables this 'fingerprint'. Bundle install again and use regular relative paths. [Learn about the asset pipeline/ SCSS for Rails 4](http://guides.rubyonrails.org/asset_pipeline.html) – socialpiranha May 29 '16 at 14:55
  • That link made for very interesting reading! One thing I did find is that the images are actually making it on to Heroku via these fingerprints within my public/assets folder. (https://pure-gorge-23608.herokuapp.com/assets/hero-000-d323633eb428852daedbdbce4ee5fd6d4710cd148f340f3ca1055bd33f116afb.jpg). – Liz May 29 '16 at 15:55
  • And no luck with the removal of those three gems. Still no background images. – Liz May 29 '16 at 16:56
  • I edited my original post with a few more variations of `asset-path` – socialpiranha May 30 '16 at 02:56
  • Did you ever figure this out @Liz? – socialpiranha Jun 02 '16 at 21:55
1

I think the main problem in Heroku is, its unable to load the bootstrap.css. You have to solve this first.

for bootstrap support, add this two gem,

gem 'therubyracer'
gem 'less-rails-bootstrap'

specify about this in application.js just after jquery_ujs reference.

//= require twitter/bootstrap 

application.css before require_tree .

*= require twitter/bootstrap

And then remove the bootstrap href references from the <head> in your view file.

I think this will help you to understand more. http://rails-magic.blogspot.com/2016/05/how-to-integrate-custom-bootstrap-theme.html

M. Karim
  • 932
  • 1
  • 9
  • 18
  • I'm using SASS, not rails. Will this work with bootstrap-sass instead of less-rails-bootstrap? – Liz May 30 '16 at 04:07
  • At first I also tried with 'bootstrap-sass', but it wasn't working at all with custom theme. But with 'less-rails-bootstrap' it worked perfectly. I think give it a try, I think it should work. (y) – M. Karim May 30 '16 at 04:13
  • Can sass and less coexist peaceably or do I have to uninstall sass? – Liz May 30 '16 at 04:20
  • better to remove the sass. with only 'less-rails-bootstrap' all the bootstrap functions was working fine after deployed in heroku. https://intense-brushlands-1551.herokuapp.com/ – M. Karim May 30 '16 at 04:26
1

Try this :

rake assets:precompile RAILS_ENV=production

Since precompilation is done in production mode only, no need to explicitly specify the environment.

Update:

Try adding the below line to your Gemfile:

group :assets do
  gem 'therubyracer'
  gem 'sass-rails', "  ~> 3.1.0"
  gem 'coffee-rails', "~> 3.1.0"
  gem 'uglifier'
end

then bundle install

hope it works :)

vipin
  • 2,374
  • 1
  • 19
  • 36
0

I see multiple issues related to your configuration. The images you posted show the 404 errors, but the errors on localhost are different from the ones on the Heroku app. The ones on the Heroku app are related to an absolute path for a CSS file, and once you resolve that errors you'll be able to see the images on Heroku.

The Heroku app is referencing an absolute path to a bootstrap file. The absolute path points to a file on localhost. Since the file exists on localhost it doesn't create the same sort of problem when viewing your app on localhost as it does on Heroku.

Taking a close look at the URL in the error messages you posted: https://pure-gorge-23608.herokuapp.com/Users/yourname/Dropbox/Code/BIA/app/assets/stylesheets/bootstrap.css Failed to load resource: the server responded with a status of 404 (Not Found)

Try searching your codebase for that absolute path. If you're on a Mac or in a similar Unix environment, ack /Users/yourname/Dropbox/Code/BIA

hint: How do I find all files containing specific text on Linux?

When you find the files that contain the absolute path references to the bootstrap file(s), change the reference so that it is a relative path instead of an absolute path.

You could also use find in files if you're on a windows localhost, but it's been a long time since I worked in windows. http://www.howtogeek.com/99406/how-to-search-for-text-inside-of-any-file-using-windows-search/

Good luck and keep at it, your web site looks promising!

Community
  • 1
  • 1
Gui LeFlea
  • 795
  • 3
  • 12
  • Look in the file name `application.scss` or `application.css.scss`. You have an absolute path in your `@import`. – Gui LeFlea Jun 01 '16 at 20:00
  • You're duplicating my answer – Jonathan Jun 01 '16 at 20:10
  • @Jonathan, I identified the specific place in the app where the absolute path is causing 404 errors, it's in `application.scss` or `application.css.scss`. I think my answer is more accurate. – Gui LeFlea Jun 01 '16 at 20:14
0

Unfortunately, between all of the wonderful answers here, I never did solve the problem. I did, however, figure out a slightly hackey workaround to use erb-linked images from the html.erb documents instead of the .css.scss documents.

I linked images this way in the HTML:

<div class="row hero-image-row">
  <div class="hero-image-outer text-center">
    <div class="hero-image-inner text-center">
      <%= image_tag 'bg-hero-000.jpg', class: "hero-image",alt: "strong man and woman lifting weights" %>
    </div> <!-- hero-image-inner -->
  </div> <!-- hero-image-inner -->
</div> <!-- row -->

Then used CSS to create a background-ish feel to the images:

.hero-image-outer {
   width: 300%;
   height: 600px;
   overflow-y: hidden !important;
   border-bottom: solid thick red;
 }

 .hero-image-inner {
   width: 66%;
   overflow-x: hidden !important;
 }

 .hero-image {
   height: 600px;
   margin-left: -50%;
   z-index: 0 !important;
 }

 @media screen and (min-width: 1100px) {
   .hero-image-outer {
     width: 100%;
   }

   .hero-image-inner {
     width: 100%;
     height: 600px;
   }

   .hero-image {
     width: 100%;
     height: auto;
     margin-left: 0%;
   }
 }

 .overlap-hero-image {
   margin-top: -500px;
   height: 500px;
 }

Definitely not perfect, and it doesn't solve the great Heroku CSS image mystery of 2016, but it works well enough for the purposes of this site. Thank yous to all of my answerers for taking the time to dig into this with me.

Liz
  • 1,369
  • 2
  • 26
  • 61
  • 1
    According to SO, github discontinued messaging, so I added you as a collaborator. Feel free to take a look or not, as I'm already using the above-mentioned workaround. However, I am always curious to learn what went wrong, as this one was quite a mystery. – Liz Jun 01 '16 at 22:39
0

I had the same problem, I was using

background: url("/assets/homebackground.jpg") no-repeat center center fixed;

on my application.css. It worked on localhost but no image was showing on Heroku. I changed the following from false to true:

# config/environments/production.rb
   YOURAPPLICATION::Application.configure do

   # your config settings
   config.assets.compile = true

   # your other config settings
end
Mark M
  • 91
  • 1
  • 6