Strange error on rendering an image in an email - ruby-on-rails

I am runnning Ruby on Rails 4.1 in production mode and I send a successful email from my web application to my GMail account. This email contains an image (the app logo) but this image is not displayed in the message body.
Logging the image request on the server side I get the following error:
ActionController::RoutingError (No route matches [GET] "/assets/my_app_logo.png")
Inspecting the email source code I get:
<img class="CToWUd" alt="MyAppName" src="https://ci4.googleusercontent.com/proxy/J8xdV03MSjJGOJh6F8T5ntqhjC2YJAiShAWJshvcOLn9THWAC5hKwp4DCLc6csuoojWlaKzPXjt-6zBAkZZvzpYMH=s0-d-e1-ft#http://www.my_app_name.com/assets/my_app_logo.png">
What is the problem? How can I solve it?
In my partial template I have:
<%= link_to(image_tag("#{root_url}assets/my_app_logo.png", :alt => MyAppName), root_url.to_s) %>
Update after #kasperite's comment
Can you post what's in application.css? also is it application.css
under assets/stylesheets?
Yes, my application.css.scss file is assets/stylesheets:
// ...
.my_app_logo { background-image: image-url("my_app_logo.png"); display: inline-block; }
// ...
However the application.css.scss is not used for rendering the email. I use the code as stated in the above partial template.

I think the problem is that after your assets get compiled for Production, my_app_logo.png does not technically exist anymore. It is actually renamed by appending a fingerprint onto the asset, so the actual asset is my_app_logo-SOME_FINGERPRINT_STRING.png.
The crux of the issue is you are attempting to do an absolute path to the image in your image_tag helper. Try doing this instead:
<%= link_to(image_tag("my_app_logo.png", :alt => MyAppName), root_url.to_s) %>
This should generate the proper link to the renamed image.

Related

Rails Wicked PDF error log (image not rendering)

I am using Rails 5 and Wicked PDF to render a PDF. On my localhost everything works fine but on my Heroku production server the image is not being rendered. First guess of course is that the image is only available on localhost but that's not the case.
If I render the same view as HTML the image is available but as PDF the image does not show.
<%= wicked_pdf_image_tag 'logo-invoice.jpg', class: 'logo' %>
It shows only a small grey square. The Rails log does not show any errors. I event tried the full URL without using Rails tags:
<img src="https://example.com/assets/logo-invoice-759b0991be66c5119a10b30680ad8902eaceacc33cfcc04afbc839d3ec404870.jpg">
Still no success. Problem is I don't know where to start debugging this?
Any ideas?
Wicked PDF can have issues rendering images from the asset pipeline. Try using the wicked_pdf_asset_base64 helper method i.e.
<%= image_tag wicked_pdf_asset_base64('logo-invoice.jpg'), class: 'logo' %>

the image does not appear in web browser using rails 5

I have problem with my app in rails . i am creating a simple blog in rails. but images not loaded on browser . i have put images on app/assets/images.
If you put file on assets folder, you should using pipeline, like:
<%= image_tag 'profile.png' %>
Or if all you want is the path to image without tag:
<%= asset_path 'profile.png' %>
Moreover, for using path without pipeline like host/img/profile.png, you should put images on:
/public/img/profile.png

Image not loading in dynamic page of Ruby on Rails app

I've done the assets pipeline for my rails project and everything is working fine, except on the dynamic posts page, like http://localhost:3000/posts/2, where images doesn't load. Everywhere else it works fine.
In console I get this error:
ActionController::RoutingError (No route matches [GET]
"/posts/assets/logo.png")
But I used src="assets/m1.jpg in image tag but in the console error the link is different!
What am I missing?
If the image tag has the following exact image path specified:
<img src="assets/m1.jpg" etc="etc">
...then the browser will treat that as a relative URL and try to look it up relative to the URL of the current page, which is http://localhost:3000/posts/2.
As a result, the browser will look for an image with this URL: http://localhost:3000/posts/assets/m1.jpg, which is exactly what's happening in your case.
Try using the image_tag helper instead:
<%= image_tag "m1.jpg" %>
Links that don't start with a / look within the same "folder" you're currently in. Thus when you're on localhost:3000/posts/2 you're in the posts 'folder' and so it looks for localhost:3000/posts/assets/m1.jpg.
If your start a link with a / it'll look from the top of the current site localhost:3000, thus /assets/m1.jpg will look for localhost:3000/assets/m1.jpg which is where you want it to be looking.
In your code you, can use the image_tag helper instead of manually writing out your image tags:
image_tag('m1.jpg') # <img alt="M1" src="/assets/m1.jpg" />
You should use the helper <%= image_tag('m1.jpg') %> and put your image in the assets/images/ directory.
The helper will generate the correct path to the image

Rails image_tag generating wrong path in production

I have a Rails 4.2.0 app that uses image_tag to display an image, for example:
= image_tag 'android_green_300_100px'
(It uses slim also)
In development mode, that generates the following img tag:
<img src="/assets/android_green_300_100px-3c57292ef62b34ed33756c2057c8c7320c22ac7fc7061576b29a97d312d954b1.png">
which works great.
When deploying to production with capistrano, the image file with the appropiate name is generated:
INFO -- : Writing
/home/app/releases/20160405210757/public/assets/android_green_300_100px-
3c57292ef62b34ed33756c2057c8c7320c22ac7fc7061576b29a97d312d954b1.png
But the image_tag method returns the image tag:
<img src="/images/android_green_300_100px">
Which obviously returns a 404 error.
If I manually access the correct image URL, it works (the image is there)
Any ideas? Thank you very much!
Pretty sure you have to use the file extension for image_tag to work in production:
= image_tag 'android_green_300_100px.png'
Add this line to your production.rb:
config.assets.digest = true

Rails 3: On a local server (# rails server), my images weren't found, although the correct location is given

I started the rails server with rails server.
My images are in public/images, i.e. the image public/images/rails.png is put into html with <img src="/images/rails.png" /> just as the image_tag helper generates it.
But by any reason, the picture isn't found. If I enter the source of the file directly into the browser, I get a routing error: "No route matches http://0.0.0.0:3000/images/rails.png".
Any help?
Yours,
Joern
Check your environments' config file, ie. config/environments/production.rb. If there's a setting config.serve_static_assets = false , the problem you described will occur.

Resources