I have a rails4 app that using carrierwave with S3 and cloudfront. I only have problem with the fallback image. When I'm using html response (<%= image_tag user.profile.avatar.url(:base_thumb), class: "profile-index-avatar" %>) with the helper everything works fine, but can't figure it out how to make it work with json response.
If I check out the html (built from json) in production on the root page the code is:
1st jbuilder: <img src="https://example.com/small_thumb_default.png">
2st jbuilder: <img src="https://example.com/assets/small_thumb_default.png">
None of these are working.
On the top of this if I go let's say to the users page then it tries to get pic like:
1st jbuilder: <img src="https://example.com/users/small_thumb_default.png">
2nd jbuilder: <img src="https://example.com/users/assets/small_thumb_default.png">.
What should I change?
jbuilder 1st version
json.array! #other_notifications do |notification|
..
json.profile_image_url notification.sender.profile.avatar.url(:small_thumb)
...
end
jbuilder 2nd version
json.array! #other_notifications do |notification|
..
if notification.sender.profile.avatar_url == "default.png"
json.profile_image_url "assets/small_thumb_default.png"
else
json.profile_image_url notification.sender.profile.avatar.url(:small_thumb)
end
...
end
uploader
process :resize_to_fit => [400, 400]
version :base_thumb do
process resize_to_fill: [85, 85]
end
version :small_thumb, :from_version => :base_thumb do
process :resize_to_fill => [40, 40]
end
def default_url
[version_name, "default.png"].compact.join('_')
end
Most likely, the file in question is inside app/assets/ folder, files inside that folder are being precompiled by default and a random digest is being added on the end of the filename. For the described behavior to work your small_thumb_default.png file should be placed inside public/ folder, so it doesn't get a digest appended on precompile. Either that or you should avoid using HTML, do it the Rails way:
# AVOID THIS
<img src="small_thumb_default.png"/>
#THIS IS PREFERABLE
<%= image_tag 'small_thumb_default.png' %>
This way you will get your view rendered with the precompiled filename.
Related
Currently I do this for my website
image_tag("logo.png")
This results
<img src="/assets/logo-c9dc9867ad75fdidmjdoehdo53di.png" alt="Logo" />
This works just fine for me. But sometimes I just need the source part of the image i.e I just need this part /assets/logo-c9dc9867ad75fdidmjdoehdo53di.png. How can I get it?
If your images are in app/assets/images, then use asset_path
<%= asset_path("logo.png") %>
# => "/assets/logo-c9dc9867ad75fdidmjdoehdo53di.png"
If your images are in public/assets, then use image_path
<%= image_path("logo.png") %>
# => "/assets/logo-c9dc9867ad75fdidmjdoehdo53di.png"
i am using wickedpdf gem to generate pdf invoice from the html code.
gems:
gem 'wicked_pdf'
gem "wkhtmltopdf-binary"
gemfile.lock
wicked_pdf (1.0.6)
wkhtmltopdf-binary (0.9.9.3)
in controller:
def show_pdf_invoice
respond_to do |format|
format.html { render :layout => "pdf.pdf.erb" }
format.pdf do
render pdf: "show_pdf_invoice", :layout => 'pdf.pdf.erb'
#render :pdf => "pdf"#, :layout => 'pdf.html.erb'
end
end
end
in views/invoices/show_pdf_invoice.pdf.erb
<img id="image" src="https://www.google.co.in/logos/doodles/2016/holidays-2016-day-2-6356741311692800-scta.png" alt="logo" />
<%= wicked_pdf_image_tag 'https://www.google.co.in/logos/doodles/2016/holidays-2016-day-2-6356741311692800-scta.png' %>
pdf is getting generated. But the images are not showing. in the place of images empty boxes are coming. unable to find the issue.
I've had the same problem, mine was fixed by removing https for http. Have you tried this? and for the Amazon S3 part: You could use gsub for that as in: gsub("https", "http")
Using Rails 5.2 with Active Storage in combination with Amazon S3 storage I had the same problem.
In development on my local machine the images rendered perfectly, but on Heroku
they were presented as small empty rectangles.
To get the url from the logo uploaded to Active Storage I used: #my_object.logo.service_url.
Which used the standard url with https. As mentioned before, replacing this with http resolved the issue.
Full code used in my pdf generator view:
<%= wicked_pdf_image_tag #my_object.logo.service_url.gsub("https", "http") %>
Two Options
1. Upgrade to wkhtmltopdf 0.12.5.
-or-
2. Install libssl1.0-dev with apt-get install libssl1.0-dev.
See this issue for more information: https://github.com/wkhtmltopdf/wkhtmltopdf/issues/3001
I am using CarrierWave to upload images. I am also using this functionality in ActiveAdmin.
I followed the RailsCast and everything is working up to the point where the RMagick gem is coming into play. Before resizing an image and just getting the image by calling:
"<%= image_tag #client.image_url.to_s %>"
When I go to resize an image, the image breaks. The code I have in my image_uploader.rb file is the same as Ryan Bates in where I am doing the following:
"version :thumb do
process :resize_to_limit => [50, 50]
end"
Then, when adding the :thumb method to my previous erb tag with the following:
"<%= image_tag #client.image_url(:thumb).to_s %>"
The images break. The interesting thing for me is that I have done this process multiple times, but this is the first time I have tried using CarrierWave and RMagick with Active Admin.
I'm currently trying to get the first frame of a gif file, resize it and save it as a jpg file.
The conversion seems fine I think. But it doesn't save it with the right file extension. It still gets saved as .gif
So when I try to open it it says can't open image, doesn't seem to be a GIF file. Then I rename the extension myself and it works.
Here is my processing code:
version :gif_preview, :if => :is_gif? do
process :remove_animation
process :resize_to_fill => [555, 2000]
process :convert => 'jpg'
end
def remove_animation
manipulate! do |img, index|
index == 0 ? img : nil
end
end
There is actually another, cleaner way to achieve this; and it is even somewhat documented in the official wiki: How To: Move version name to end of filename, instead of front
Using this method your version code would look like this:
version :gif_preview, :if => :is_gif? do
process :remove_animation
process :resize_to_fill => [555, 2000]
process :convert => 'jpg'
def full_filename(for_file)
super.chomp(File.extname(super)) + '.jpg'
end
end
def remove_animation
manipulate! do |img, index|
index == 0 ? img : nil
end
end
So ... I finally found a solution after hours of headaches why this didn't work. Turns out you have to touch/create a file first in order to make this work. I also switched from RMagick to Mini Magick. Not for a particular reason just tried it out if it would work with MiniMagick but I still had the same problem. Here is my new process code with Mini Magick:
version :gif_preview, :if => :is_gif? do
process :gif_to_jpg_convert
end
def gif_to_jpg_convert
image = MiniMagick::Image.open(current_path)
image.collapse! #get first gif frame
image.format "jpg"
File.write("public/#{store_dir}/gif_preview.jpg", "") #"touch" file
image.write "public/#{store_dir}/gif_preview.jpg"
end
I just don't understand why there is really 0 documenation about this ...
I've got a rake task which uploads images I've cached from an API to my S3 bucket. In my view, I try to output the image but it just doesn't appear to work. What I want to do is cache the images onto my filesystem, send them to S3 and I want to use the location of the image from my S3 bucket rather than my filesystem. My code looks like below:
In my rails console, I do this just to check the image url:
1.9.3p125 :002 > a.image
=> http:://s3-eu-west-1.amazonaws.com/ramen-hut/pictures/1.jpg?1343645629
1.9.3p125 :003 >
I use Paperclip in my app, is it supposed to add the url as "http:://"? Seems rather weird. The code in my index.html.erb looks like this:
<li>
<%= movie.title %>
<%= image_tag movie.image.url %>
</li>
But this results in the following html:
<li>
Cowboy Bebop
<img alt="1" src="/assets/http:://s3-eu-west-1.amazonaws.com/ramen-hut/pictures/1.jpg?1343645629">
</li>
Why does it include the '/assets'/ before my URL?
I configured Paperclip to set up the image url for my European S3 Bucket following a tutorial. So in my environment.rb, I've got this:
#Signature correction for Paperclip and AWS
AWS::S3::DEFAULT_HOST = "s3-eu-west-1.amazonaws.com"
And I've got an aws-signature.rb file in my initialisers directory with this code:
#Makes Paperclip use the correct URL for images
Paperclip.interpolates(:s3_eu_url) { |attachment, style|
"#{attachment.s3_protocol}://s3-eu-west-1.amazonaws.com/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, "")}"
}
There's a problem with the URL : http::// instead of http:// so image_tag doesn't know it's an absolute URL.
How do you generate these URLs? Gem or your own code?