Rails 4.2 not loading correct image - ruby-on-rails

I'm working on the depot_a app from Agile Web Development with Rails 4. Following the steps in Task A, all images are in app/assets/images/:
[~/projects/try_ruby/depot]$ ls app/assets/images/
cs.jpg logo.png rails.png rtp.jpg ruby.jpg
In app/views/products/index.html.erb, the code to present the image:
<%= image_tag(product.image_url, class: 'list_image') %>
Instead of accessing asset/cs.jpg, it actually generates the obfusticated image path (see the screenshot): /assets/cs-436e7e2d621d24ea1d19769362306ecdec83fcd62d5d45f135adca63e6a0f9c5.jpg.
Why is that?
The jpg contains error:
EDIT:
My rails version is 4.2. But the book's depot app is based on Rails 4.0. So I downgraded Rails to 4.0 and re-did the steps again (regenerated scaffold, db seeding, etc.), then the jpg photos are shown correctly. It seems like a version thing. But I still don't know why jpg doesn't load in Rails 4.2.

The obfusticated image path /assets/cs-436e7e2d621d24ea1d19769362306ecdec83fcd62d5d45f135adca63e6a0f9c5.jpg. could be from the public/assets directory and not app/assets directory. The possible reason is that you precompiled your assets.

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.

Ajax_pagination gem loading image missing in Rails 4 production

I was using this gem successfully in production until moving to Rails 4. Now the loading image is not working properly. I have reached out to the developer, without assistance, so I would like to ask your help on this one.
Here is the file which I believe is involved:
https://github.com/ronalchn/ajax_pagination/blob/1297da84ead61f206e30f3d6bae2da6a551fed14/lib/generators/ajax_pagination/assets_generator.rb
Can this repo be modified to correctly show the image in Rails 4?
At the moment, I'm just seeing a broken image link.
The fix was simple in the end I just downloaded the original image:
https://github.com/ronalchn/ajax_pagination/blob/master/lib/assets/images/ajax-loader.gif
and put it into my existing assets/images directory! Bingo!

Anomalous Image Behavior In Rails 3.2.3

I'm new to RoR and in my Rails 3.2.3 app without the sprockets gem installed, I've used "image_tag" to display "example.png" which exists in "public/images," like so:
<%= image_tag "example.png" %>
It works, yet, so far all attempts to display any other image in the same directory fail. For example,
<%= image_tag "embassy.png" %>
...fails, even though the file exists. It's like the app will only display "example.png" and no other image in the same directory. The permissions are the same on all files. The assets pipeline is enabled per,
config.assets.enabled = true
...in "config/application.rb." I've grokked a few resources from the API to random articles and nothing has helped me understand this odd behavior. Can anybody explain either,
1) Why and when Rail behaves like this?
and/or
2) How to fix it?
Try using the app/assets directory for storing images, they will be copied into public/assets when needed. The images can be loaded from there.
See also the asset pipeline for more information.

Rails 3.1 image asset timeout

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.

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