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

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'

Related

VSCode set width for code formatting of arrays etc

If my array goes over a certain length:
const List hts = [200, 195, 190, 185, 180, 175, 170, 165, 160, 155, 150, 145];
VSCode formats it like this:
const List hts = [
200,
195,
190,
185,
180,
175,
170,
165,
160,
155,
150,
145,
140
];
I'd rather it didn't since it makes a short array take up half the page. How can I change this?
Also I get even weirder formatting like this which means if I comment out a line it only comments out half the line:
This:
Text('RESULTS', style: kMainText),
Text('Additional', textAlign: TextAlign.center, style: kSmallText),
Gets formatted as this:
Text('RESULTS', style: kMainText),
Text('Additional',
textAlign: TextAlign.center, style: kSmallText),
The second line is one line but I have to comment it out as two lines. It seems like there's some kind of 'length' setting somewhere that I could alter to avoid this. How can I edit all this behaviour?
The Dart VS Code extension uses dart_style for formatting, which ships in the Dart SDK.
There is a setting called "maximum line length" which can be configured in your VS Code settings in the Dart section which will allow lines to be longer before they wrap (though note that this applies to all lines, not just lists).
There's an issue requesting an alternative (more customisable) formatter in VS Code here:
https://github.com/Dart-Code/Dart-Code/issues/914
Please add a 👍 to the issue if it's something you'd like to see.
The dart formatter will preserve line breaks in an list if there's a comment line at the head of this list.
For example, to format a very long list in Dart:
[
// yeah
a,b,c,
d,e,f,
];
See the test case (link) from official dart style repository (https://github.com/dart-lang/dart_style)

TCDPF render a pdf with a png image, problem with transparent png

If i render a pdf with a png file, i get the following error.
What's the problem with this?
Warning
Message: imagepng(/...examplepathfolder.../tmp/__tcpdf_172a5566d5e348eba254a398c0909340_imgmask_alpha_2e51083373b9767f3e6183dfb1d0d48e): failed to open stream: No such file or directory
Filename: tcpdf/tcpdf.php
it seems that is a problem with a transparent png file if i try the same with a jpeg there is no error.
There is an example to work with transparent files, https://tcpdf.org/examples/example_042/
For me i wont't understand what i have to change with my code
$info_left_column .= pdf_logo_url();
$pdf->MultiCell(($dimensions['wk'] / 2) - $dimensions['lm'], 0, $info_left_column, 0, 'J', 0, 0, '', '', true, 0, true, true, 0);
pdf_logo_url() returns the absolute path of the png file.
omg i found the problem.
So, under normal use the temp-folder ("K_PATH_CACHE") is not in use for tcpdf. If you use a png the temp-folder is important to create a second image to prepare problems with the alpha-channel.
So, in my installation was a wrong temp-folder, so i changed it and now all works fine :-)
As I showed in the picture, just uncomment this line:
//define ('K_PATH_CACHE', '/tmp/');

React Native thinks image is a module

This may seem like a duplicate but I've followed the answers to maybe a dozen similar questions on Stack and none of the solutions are working for me.
The React Native docs say load to local images like so:
<Image source={require('./my-icon.png')} />
I've placed the image 3D-home2#x.jpg in the same folder as the JS file:
I also tried placing it at the project root:
I also tried placing it in Images.xcassets and loading it like require('image!3D-home').
In between these efforts, I cleared the module cache several times with yarn start -- --reset-cache. Did a full XCode project 'clean' several times and rebuilt the app. No matter what I do I get some variation of this error (project name redacted):
What am I doing wrong here? It seems like I'm following the instructions exactly but I cannot get an image to load.
<Image
style={{
position: 'absolute',
width: '100%',
height: '100%',
top: 0,
left: 0,
zIndex: 10
}}
source={ require('./3D-home#2x.jpg') }
/>
The solution was to remove #2x from the file name. This is because React Native offers the ability to automatically swap out images using suffixes, so I was unwittingly trying to do that incorrectly or something:
You can also use the #2x and #3x suffixes to provide images for
different screen densities.
https://facebook.github.io/react-native/docs/images.html

default image from localhost rather than loading from cloudinary

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;'

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