default image from localhost rather than loading from cloudinary - ruby-on-rails

I am using cloudinary to store user avatars in my rails 4 application. I also have a placeholder image in my image assets. I wanted to know that if a user hasnt uploaded his avatar, how do i load it from the localhost.
As of now i have to add checks as
- if user.avatar.present?
= cl_image_tag(user.avatar.filename, width: 46, height: 46)
- else
= image_tag 'default.png', style: 'width:46px; height:46px;'
i can specify a default image as
= cl_image_tag(user.avatar.filename, width: 46, height: 46, default: 'default.png')
but the default image has to be stored on cloudinary. I dont want to store it on cloudinary since cloudinary charges for data transfer. hence, i have stored 'default.png' is stored in assets. 'cl_image_tag' is a helper provided by cloudinary to load images from there.

I solved the problem as below
Firstly, the default images need to be saved on cloudinary.
eg
<%= cl_image_tag("non_existing_id.png", width: 100, height: 100, default_image: "avatar.png") %>
if you dont want to load default image from cloudinary but from our localhost, add the below code in a partial and use this partial.
- if user.avatar.present?
= cl_image_tag(user.avatar.filename, width: 46, height: 46)
- else
= image_tag 'noPic_80.png', style: 'width:46px; height:46px;'

Related

Encoding problem when adding text to existing PDF with CombinePDF

(Rails 6.0.2.2, ruby 2.7.1, combine_pdf 1.0.18)
I'm currently trying to write some text to an existing PDF with the CombinePDF gem. Unfortunately I'm running in some encoding problems.
I'm loading the existing PDF:
pdf = CombinePDF.load "#{Rails.root}/public/pdf/base.pdf"
Then I'm adding text to it:
pdf.pages[0].textbox "Straße", height: 20, width: 160, y: 527, x: 215, font_size: 12, box_color: nil, text_align: :left, text_padding: 0
When generating a new pdf out of it:
send_data pdf.to_pdf, filename: "output.pdf", type: "application/pdf"
the string gets displayed as StraˆŸe, so the ß isn't displayed correctly.
I also tried to replace it with unicode literals (\xc3\x9f) without any effect.
Anybody has an idea what else to try?
If you use HexaPDF you could do something like this:
require 'hexapdf'
require 'stringio'
doc = HexaPDF::Document.open("#{Rails.root}/public/pdf/base.pdf")
doc.pages[0].canvas.font("Helvetica", size: 12).text("Straße", at: [215, 527])
outio = StringIO.new(''.b)
doc.write(outio)
Just make sure that you use a font that contains glyphs for all characters you want to use. So better use a TrueType font instead of the builtin Helvetica (you can just provide the path to a TrueType font to the #font method).

Rails and SLIM html image_tag points to images instead of assets

I have a scenario where I need to send emails using a template written in SLIM. I fixed my environment/development.rb with the following config that I find necessary (also based from my research)
config.assets.precompile += %w( print.css )
config.serve_static_assets = true (still using Rails 4.1.16)
config.action_mailer.default_url_options = { host: 'http://localhost:3000' }
config.action_controller.asset_host = 'http://localhost:3000'
config.action_mailer.asset_host = config.action_controller.asset_host
I know it's pointing correctly to my root url on this step. Then In my mailer.html.slim I have something like this:
div align="left" style="border-collapse: collapse; padding-left: 30px;padding-top: 30px;padding-bottom: 30px;float:left;"
a href="http://#{App.url}"
= image_tag "images/logo-old.png", style: "display:inline-block", border: "0", alt: App.name, width: 139, height: 44, title: App.name
I tried the following already:
= image_tag "logo-old.png"
= image_tag "assets/images/logo-old.png"
= image_url "logo-old.png"
Nothing's working but good thing its now showing the Alt to know its still working by its path.
The current = image_tag evaluates to :
src="https://ci5.googleusercontent.com/proxy/83z0D3PVOqx-FwFFMwztfYBs5CSWHiURyxSUP6cZ3dq7Zo47k9mNPgotrijVmmWGxPHqblRTGCePqN4RrfEhHh_665
MdAQ=s0-d-e1-ft#http://localhost:3000/images/images/logo-old.png"
Any idea why the app can't find the normal pipeline path?
Instead of images/images/logo-old.png I need the assets/images/logo-old.png
Edit:
So for result comparison, if I do image_url "logo-old.png" the path results to /assets/logo-old.png
If I do image_url "images/logo-old.png" the path results to images/images/logo-old.png
My image is in app/assets/images/logo-old.png
1) is http://localhost:3000/images/images/logo-old.png the correct folder structure of your app/assets/images/images/logo-old.png ?
2) did you add the app/assets/images to the asset pipeline?
How to add folders to asset pipeline
Javascript does not work from a bootstrap template in rails
3) did you check with Rails.application.config.assets.paths the default search paths in your rails console, did you try to organize correctly the images to be stored inside app/assets/images folder and added that folder to the asset pipeline?
then you should be able to do image_url "logo-old.png"
Thanks!

How can I display a svg in prawn without saving a .svg file before?

For this moment, I generate a svg file and then I read it and I display it in my pdf:
qr = Barby::QrCode.new('test')
outputter = Barby::CairoOutputter.new(qr).to_svg
File.open('myfile.svg', 'wb'){|f| f.write outputter }
pdf.svg IO.read('myfile.svg'), width: 50, height: 50
My question is: how can I display my svg without saving a .svg file before. I know there is a css property that allow that but I can't use css with prawn...
Any ideas?
It's working like that:
pdf.svg outputter, width: 50, height: 50
thanks #Mark Thomas

only display an image if it's present in rails app

I have a blog page on my website and some posts have images and some don't. If there wasn't an image it was showing an error so I changed the code to only show if an image is present
.post_body
= #post.body.html_safe
- if #post.image
= filepicker_image_tag #post.image, w: 208, h: 208, fit: 'clip'
On my localhost, this worked well to solve the problem and nothing was displayed if I hadn't added an image to the post. But when I push it to heroku, my code changes haven't made any difference. If there is no image on the post it shows a broken image icon.
Does anyone know why this would be fine in localhost but not online?
Thanks
Add condition, like this
filepicker_image_tag #post.image, w: 208, h: 208, fit: 'clip' unless #post.image.blank?
or
.post_body
= #post.body.html_safe
- if #post.image.present?
= filepicker_image_tag #post.image, w: 208, h: 208, fit: 'clip'

Pre-compiling issues using the Cloudinary gem

I'm using Cloudinary in my application and it works like a dream in my dev environment. However, when I deploy the app to Heroku and visit a page that has cl_image_tag, it gives me the "something went wrong" page.
When I look at the log it gives me this error:
ActionView::Template::Error ( isn't precompiled)
This is the line causing the problems:
<%= cl_image_tag(t.image, width: 175, height: 175, crop: :fill, gravity: :faces, border: { color: '#CCC' }) unless t.image.nil? %>
Any help is greatly appreciated.
This error is thrown when the image is nil. Maybe the image attribute of t is nil in this case?

Resources