Carrierwave to upload image - ruby-on-rails

I'm using carrierwave to upload an image, this works fine up to now.
What i want to do is show the preview of the file to be submitted on the form after having selected the image.
At the moment the code I have is showing the previous image.
<%= image_tag(#book.cover_url(:thumb)) if #book.cover_url %>
how do I show the selected image rather than the image already uploaded?

You don't.
And that has nothing to do with Carrierwave but rather with the fact that the image is not yet sent to the server by your browser so no part of the Rails stack is coming into play here.
What you need is a way to access the image inside the <input type="file"> and display it to the user.
This is possible by using the FileAPI that is part of HTML5, but it's only supported in modern browsers (Webkit/Gecko), and the API is not completely finished yet so it's kind of a changing target.
I would suggest you look into tools like plupload to upload the image to the server without submitting the form and then displaying that through JavaScript.

Try http://jasny.github.io/bootstrap/javascript/#fileinput
It has a nice solution for displaying the image without the upload.

Related

Images disappear in Rails app using ActiveStorage

I'm encountering a very weird bug in my Rails app.
I'm using ActiveStorage to store images on S3 and images sometimes appear but sometimes don't.
If you scroll down a little bit, one card ("Yoga retreat") doesn't have a thumbnail.
My code:
<a href="#" style="background-image: url('<%= rails_representation_path(experience.thumbnail) %>')">
experience.rb
def thumbnail
self.cover.variant(resize: "300x300").processed
end
What am I doing wrong?
UPDATE
When I deploy the problem goes away (I can see the image), even if the changes have nothing to do with the image.
If I then add another card, the image doesn't appear again, unless I deploy again.
UPDATE 2
I've found out the problem but I don't know how to fix it. The problem is the cache. It has nothing to do with ActiveStorage. The image is not displayed because it points to the old url. In fact, if I clear the cache, everything works.
In my view I use collection fragment caching
<%= render partial: :experience, collection: #experiences, cached: true %>
Why does it not update when I update my record?
I've noticed it doesnt't just not update my image, it doesn't update my title too if I change it.
First off, awesome app!
Second, you have to check out your terminal. ActiveStorage doesn't delete its previous state, which means, if you add another image to an existing image, there will be an error, because the storage wasn't null. I think that your 'yoga' picture is somehow connected in the code to any further uploads, or that you use active_storage the wrong way.
My suggestion is to run
attachment.purge_later()
before you attach the image (inside your controller). Also, are you using turbolinks? Make sure to use
$(document).on(turbolinks:load, function(){})
before you run all your javascript.
If that didn't helped, post your controller that handles the upload, your storage place and the configuration (make sure to NOT post your credentials!).
Also, what host are you using? Try to go into the rails console when the image disappears and get the id of the 'yoga' image and see if it becomes null.
Last thing: i could see the image when i visited your site.
Greetings!

Images tags not showing in wicked_pdf gem

I have a rails app, in which i store some images in AWS, and show them in a PDF report. When there is no images to be shown, i show a placeholder, like this one here: .
The problem is: the image is not show in the PDF file. When in debug mode, the image is shown as usual, but never in my PDF! The link to the image is like this: https://dl.dropboxusercontent.com/u/4096865/missing.png
Edit 1:
I tried to include the image like this: image_tag("https://dl.dropboxusercontent.com/u/4096865/missing.png")
The solution i've found was simple, but prevented me from using dropbox for storage: it must be a http url. So, when i uploaded it to AWS S3, it worked!
So, this here works perfectly: image_tag("http://s3-sa-east-1.amazonaws.com/base-fisc-prod/missing.png")
This is an issue with images stored in a HTTPS link, the easiest way to solve this is to store the images in a HTTP link.
If you must store them in HTTPS, use this solution provided here

Tinymce rails image upload - how to delete images if post isn't created

Basically I fallowed this documentation: tinymce rails image upload
It works, but the problem is that It saves every image in the db before the actual post is created. Thus even if the post isn't finished or saved, the images still remain on the server. And that is just waisting space on the server.
Does anyone know a better way for image upload with tinymce ?

forge.topbar.setTitleImage(URL,success,error); dynamic image .. fileCache?

I have been told this is possible to do but cannot get this to work.
Basically I am trying to cache and image using forge.file.cacheURL()
Now I can get the file just fine and display it in the page by creating a new Image object but what I want to do is use this cached image to change the image in the topbar.
When ever I try to do it I get an error saying "file not found" and after reading the docs a little more it seems that trigger may have "src/" coded into the class because if I put just "image/logo.png" the logo.png will show up in the header because its part of the app package.
I guess the question is.. Is my assumption correct?
Thanks!
The native UI elements (topbar/tabbar) load images directly from your apps package, not through a URL in the same ways images are shown in the webview.
What this means is that currently you can only use images included in your app in the topbar/tabbar modules.
in case anyone is interested in this, the ability was added a while ago to the API so you can now use a cached image or filesaveURL feature of forge to change the header image dynamically.

Avoid caching after image update for Paperclip

I am uploading images to S3 storage using paperclip gem. It uploads successfully.
In my application, an user can change his profile photo. If user do the profile photo change, the new image will get uploaded successfully. The problem is the browser will show previous photo only after the upload. Only after a force refresh by the hitting ctrl + F5 will the new image display.
Note: image is uploaded using form post. I guess it's related with caching but I don't have clue where I can change my code. Please help me this out. Thanks.
What property of your image object are you using as the src of your image? If you use user.image.url (assuming that you have a User model and that the Paperclip field's called image) then the URL to the image on S3 should have a number in the querystring that acts to stop any caching so that you see the updated image immediately. For example the URL for the image should change from
http://s3.amazonaws.com/bucket/images/5/original/pic.gif?1250184309
to
http://s3.amazonaws.com/bucket/images/5/original/pic.gif?1250184348
which should stop the browser caching the image.

Resources