Images disappear in Rails app using ActiveStorage - ruby-on-rails

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!

Related

Rails, tinymce-rails multiple images path breaking because of "../../../"

Hi everyone I am trying use tinymce-rails and and also have an upload form. This works really well since I can call the images anywhere. Just about everything is working great. The images upload, the tinymce styles stuff.
Now the weird thing is tinymce keeps adjusting the file paths to add "../../../" and this breaks the photos when editing the text. It would actually work fine if it would just go up one more "../" count on the edit page, the pathing does work on the show page.
Does anyone have any direction they can give me?
As I can't know the source of this, I can provide a workaround.
In your model, let's call it Post, you might need to replace stuff to the field containing the html, let's call it body:
before_save {
body.gsub!(/(\.\.\/)+/,"..\/")
}

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.

Image in rails asset pipeline loading indefinitely, but still appearing

Okay so basically, I have a file in my asset pipeline (http://myapp.dev/assets/avatars/default-avatar.jpg). When I visit the URL, the image loads, but continues to act as though it's transferring data (the chrome spinner is spinning). If I access the same file in production instead of development, it works just fine. In addition, no other files have this issue, it's only that ONE jpeg in that ONE folder.
I have no clue what this could be. Can anyone help?
EDIT: This is what the network activity looks like
I had the same issue (the image would show up but continue to load for a long period of time, then the transfer would ultimately fail and the image would appear broken).
Saving the image again in Photoshop resolved it for me, as you mentioned in your comment above. Must have been something wrong with the image encoding.

image_tag often causes images to not load

I just started using the asset pipeline correctly in rails 3, and am using the image_tag helper instead of just hard-coding <img>'s. At almost every page load, many of the images in the layout don't load. If I refresh a couple times, they will load, but the result is that most of the time the page is a jumbled mess. Has anyone had any experience with this, and how do I make it stop?
The image_tag per se doesn't do anything other than generate the <img> tag for you with the right path etc. If you view the source code that is served up to the browser there shouldn't be anything significantly different between your image tags, whether they've been hard-coded or not.
When you get a page that is a jumbled mess, view source and copy the path to one of the broken images. Construct that url fully and put it into your browser to see if you can retrieve just the image using the path given in the html image tag. If image retrieval is patchy then it is something to do with how your assets are being served, not the rails image_tag helper.
Also look at whether there is any caching going on - perhaps some images were cached before they were properly saved and the browser has cached the fact there is a 'missing image' and future requests for that image are being broken. Does hard-refreshing the page help to load the broken images?

Carrierwave to upload image

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.

Resources