Rails 3.1 image asset timeout - ruby-on-rails

I'm having intermittent issues with images on Rails 3.1.
The file in question is a simple PNG; all other images are working correctly. If I vist the asset path directly the image works as expected. Restarting the server (Webrick, in development) does not help. Clearing the browser cache does not help.
When I reload the page the image shows but the browser indicates it is still loading. After 30 seconds the request times out and the image is replaced with a broken image indicator.
Here are a few supporting images:
https://skitch.com/matt_darby/fahke/oops-google-chrome-could-not-connect-to-localhost-3000
https://skitch.com/matt_darby/fahkp/oops-google-chrome-could-not-connect-to-localhost-3000
https://skitch.com/matt_darby/fahkw/contact.html.haml-documents-code-protectedmethod.com-app-views-pages-vim

I too had this issue and Richard Hulse's comment above to his own answer: "delete the files in tmp/cache/assets and restart' fixed it for me. Thanks Richard.

I suspect this is not working because you are running in production mode locally, or have digest turned on in your config.
In your view you should be using the asset_tag helper:
%img{ :src => asset_path('/assets/phone.png') }
This ensures that the correct path (with the fingerprint) is rendered into the view.
The code you have will work in development mode (or without digests), but will break when they are on, or in production.
If this is an upgraded app, check the Rails asset pipeline guide for the correct stuff to paste into your config files.

Related

Production error: "image is not present in the asset pipeline" although it is there and working in dev environment?

I'm new to devops and can't figure out what's going on. Today we pushed a branch to production where we are using new images for a view.
Everything worked fine in development environment, but when it was promoted to production we started getting errors related to the new images not being found in the asset pipeline
<div class="text-center my-10">
<%= link_to t("url.static_site") do %>
<%= image_tag "logo-white-no-background" %>
<% end %>
</div>
error:
The asset "logo-white-no-background" is not present in the asset pipeline.
I've tried running bundle exec rake assets:precompile locally to emulate what happens in production (as I understand everything under /assets is precompiled and served by the web server) and I don't get any errors.
The only thing that I find different in the docs is that I'm not passing the file extension in the image_tag, although like I said it is working find in development (and actually breaks if I add the extension in that environment).
Should I try forcing a pre-compile on production to see if it is loaded correctly?
What else can I try?
You will need to add the extension to make it work in production!
It shouldn't break it in development, make sure you run rails assets:clean and rails assets:clobber to clean out the compiled assets and that you have the right file extension!
The extension is a hint to not only browsers or but also some backend servers. Nobody forces developers to add the extension; however, you can see adding the extension as a good coding habit.
For example, your image is jpg, but the browser shows it as png. You found the image shown correctly maybe it's just happened to that browser guessed the file type correctly. In some scenarios like downloading the file from S3, if there is no extension provided, it will be served as a pdf file.
Btw, about your problem, because it didn't show the image in the production environment. You can first check the src from by inspecting the source code. If the browser cannot find the image file via that URL, the URL is just invalid. After inspecting the image's URL, you can try to generate the right URL then.

Cloudfront on rails not showing new images

I'm using cloudfront as my CDN in my Rails app. I created my distribution and changed the enviroment file to enable the asset host.
Everything was working fine until I made a new deploy that included 3 new images. After restarting, everything looks fine but the 3 images. If I get the cloudfront URL and change the domain for my rails app domain the images load just fine, but if I use the CloudFront domain the images look like they weren't found.
Any ideas why this is happening? If I undestand correctly CloudFront doesn't have a delay, it loads the image as soon as the first request comes in.
run rake assets:precompile then upload it.
or check the image path
it should begin with "/" like
<img src="/images/img.jpg"/> if you are images in public assets
I found the problem. Sometime between the precompile and the restart of the server someone made a request and Cloudfront couldn't find the image so that's why it wasn't showing. I changed the image name, re-depoyed it and everything is fine now.

Rails with Twitter Bootstrap: still serving an old asset

Going nuts here. I'm developing a rails app, and I'm using the twitter-bootstrap-rails gem in order to include the Twitter Bootstrap styles in my app. This gem generates a file called 'bootstrap_and_overrides.css.less' in app/assets/stylesheets, which I have been using to modify some of the bootstrap variables and include my own CSS overrides.
Everything has been working fine until today. For some reason, the changes I am making to this file today are getting saved to the file, but Rails is still serving the old version of the file! I've searched and found no precompiled versions of the file anywhere (nothing in public/assets)...only the one in assets/stylesheets which I have been modifying. Everything looks fine as far as the directories within the app go, but then when I start the rails server, load the page, and use the element inspector to look at the stylesheets, it's using an old version of 'bootstrap_and_overrides.css.less' with rules that I have deleted. I've turned of the cache in my browser, and tried it in 4 different browsers too, so I'm pretty sure this isn't a result of browser caching.
The rails asset pipeline just seems to serving a version of the file that doesn't exist! Does anybody have any ideas why this might be happening?
Fixed it.
The asset pipeline was storing a cached version in tmp/cache.
I ran rake tmp:clear, which deleted all the files in there, and then rails served the version of *bootstrap_and_overrides.css.less* that I wanted.
Why the cached version suddenly stopped getting updated is beyond me. Arrghhhh!

Rails 3.1 .css.less caching error

I am quite new with Rails and I am having some irritating problems with caching of css files.
I have a .css.less file with imports inside it. It's the only stylesheet the app includes, so the other files get imported only once and by this unique stylesheet.
One of those imported .css.less stylesheets seems to be cached somewhere, because does not change in the browser when I change it's source.
I can only see the changes I made if I change something in the root stylesheet.
I have the server in development mode, so the caching should be off. I have also used <%= stylesheet_include_tag "style", :cache => false %>
I tried with Chrome and Firefox, with and without clearing their cache too. Always the same result, if I work only on that file the css the page receives when reloaded doesn't have the changes...
I also stopped the server and rm everything in the tmp folder of the app. No changes.
I am using Rails 3.1 with Ruby 1.9.3, with the less-bootstrap-rails gem. Both the root stylesheet and the imported one have .css.less extension.
What am I missing?
Thank you!
This is an area where I think the asset pipeline is broken, but I don't think there's a good fix.
If I remember correctly, to get changes in files you've included/required in your .css.less file, you need to change the .css.less file itself.
I had this on Rails 4.0.8, infuriating. The config changes mentioned above didn't help. Here's what seems to have fixed it for me:
Ensure NO FILES share a base name. For example, you have a reports.css.less and a reports.js.coffee? Doesn't matter if they're in the same directory or not. Rename or delete one of them. (I changed it to reports-styles.css.less).
Blow away your cache: rm -rf tmp/cache
Restart your Rails app.
This appears to be a decent fix but, since I don't know what's actually going on, this could be totally false and it's just working by coincidence now. Sorry this answer isn't more rigorous!
I've just came across the exact same problem.
I found that if you rename your *.css.less file (the one with the imports inside) to *.less, then this weird cacheing problem gets resolved.
Add this to your config/application.rb
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
See more at: Ruby on Rails Guide: Asset Pipeline

Rails 3.1 and Image Assets

I have put all my images for my admin theme in the assets folder within a folder called admin. Then I link to it like normal ie.
# Ruby
image_tag "admin/file.jpg" .....
#CSS
.logo{ background:url('/assets/images/admin/logo.png');
FYI. Just for testing I am not using the asset_path tag just yet as I have not compiled my assets.
Ok all good so far until I decided to update an image. I replaced some colors but on reload the new styled image is not showing. If I view the image directly in the browser its still showing the old image. Going one step further I destroyed the admin images folder. But it has broken nothing all the images are still being displayed. And yes I have cleared my cache and have tried on multiple browsers.
Is there some sort of image caching going on? This is just local development using pow to serve the pages.
Even destroying the whole images folder the images are still being served.
Am I missing something?
In 3.1 you just get rid of the 'images' part of the path. So an image that lives in /assets/images/example.png will actually be accessible in a get request at this url - /assets/example.png
Because the assets/images folder gets generated along with a new 3.1 app, this is the convention that they probably want you to follow. I think that's where image_tag will look for it, but I haven't tested that yet.
Also, during the RailsConf keynote, I remember D2h saying the the public folder should not have much in it anymore, mostly just error pages and a favicon.
You'll want to change the extension of your css file from .css.scss to .css.scss.erb and do:
background-image:url(<%=asset_path "admin/logo.png"%>);
You may need to do a "hard refresh" to see changes. CMD+SHIFT+R on OSX browsers.
In production, make sure
rm -rf public/assets
bundle exec rake assets:precompile RAILS_ENV=production
happens upon deployment.
For what it's worth, when I did this I found that no folder should be include in the path in the css file. For instance if I have app/assets/images/example.png, and I put this in my css file...
div.example { background: url('example.png'); }
... then somehow it magically works. I figured this out by running the rake assets:precompile task, which just sucks everything out of all your load paths and dumps it in a junk drawer folder: public/assets. That's ironic, IMO...
In any case this means you don't need to put any folder paths, everything in your assets folders will all end up living in one huge directory. How this system resolves file name conflicts is unclear, you may need to be careful about that.
Kind of frustrating there aren't better docs out there for this big of a change.
In rails 4 you can now use a css and sass helper image-url:
div.logo {background-image: image-url("logo.png");}
If your background images aren't showing up consider looking at how you're referencing them in your stylesheets.
when referencing images in CSS or in an IMG tag, use image-name.jpg
while the image is really located under ./assets/images/image-name.jpg
http://railscasts.com/episodes/279-understanding-the-asset-pipeline
This railscast (Rails Tutorial video on asset pipeline) helps a lot to explain the paths in assets pipeline as well. I found it pretty useful, and actually watched it a few times.
The solution I chose is #Lee McAlilly's above, but this railscast helped me to understand why it works. Hope it helps!
The asset pipeline in rails offers a method for this exact thing.
You simply add image_path('image filename') to your css or scss file and rails takes care of everything. For example:
.logo{ background:url(image_path('admin/logo.png'));
(note that it works just like in a .erb view, and you don't use "/assets" or "/assets/images" in the path)
Rails also offers other helper methods, and there's another answer here: How do I use reference images in Sass when using Rails 3.1?

Resources