Active Storage _blob partial not being respected or loaded - ruby-on-rails

I am trying to edit app/views/active_storage/blobs/_blob.html.erb, the partial file created when installing active_storage which allows you to customise how blobs appear inside action_text output.
On other projects, this has worked without issue, on a new Rails 7 project it would appear that the partial added during install is being ignored, as such no edits are coming through.
I can see this by making a small text change (so not adding an HTML element which may get stripped).
For example, I've added "this is a test" in a few places, this does not show:
app/views/active_storage/blobs/_blob.html.erb:
<figure class="attachment attachment--<%= blob.representable? ? "preview" : "file" %> attachment--<%= blob.filename.extension %>">
<% if blob.representable? %>
<%= image_tag blob.representation(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]) %>
<% end %>
<figcaption class="attachment__caption">
<% if caption = blob.try(:caption) %>
<%= caption %>
this is a test
<% else %>
<span class="attachment__name"><%= blob.filename %></span>
<span>this is a test</span>
<span class="attachment__size"><%= number_to_human_size blob.byte_size %></span>
<% end %>
</figcaption>
</figure>
In other projects, when you view the log output in development it will correctly say "rendered app/views/active_storage/blobs/_blob.html.erb", on this project it doesn't.
File uploads work via actiontext, and rendering the file works by way of the built-in partial, but I can't get the customisable version to be viewed at all.
This is happening on both development and production environments, so I don't think it is anything like a weird eager loading/caching issue.
Does anyone have any ideas as to where I could start looking to debug this further?
rails: 7.0.2.3

Related

Ruby on Rails 7, Action Text, Rich Content. Image in rich_content field does not render in View

Rookie trying to follow example in Rails Guide for Action Text using Trix editor
App works great locally but images in rich_content field appear as icons in Views when run in production on Heroku - and on Flyio as well.
I understand about the temporary nature of file system on Heroku; but, even right after saving [the blog article], only the image icon is displayed in View. When user clicks to edit, the image is there. Maybe 30minutes later the image is gone but I believe that is a different issue.
I tried connecting an AmazonS3 bucket - on Heroku; but there was no change. [Full- disclosure: an examination of the bucket showed 0 contents so I didn't get that to work]
I tried on Fly.io. Created a volume for disk storage there but behavior is the same: icon displays in View and image displays in Edit.
I am using gem image-processing. As said, all works fine locally.
This is code of _blob.html.erb. I confirmed with the debugger that blob.representable? is true
<figure class="attachment attachment--<%= blob.representable? ? "preview" : "file" %>
attachment--<%= blob.filename.extension %>">
<% if blob.representable? %>
<%= image_tag blob.representation(resize_to_limit: local_assigns[:in_gallery] ?
[ 800, 600 ] : [ 1024, 768 ]) %>
<% end %>
<figcaption class="attachment__caption">
<% if caption = blob.try(:caption) %>
<%= caption %>
<% else %>
<span class="attachment__name"><%= blob.filename %></span>
<span class="attachment__size"><%= number_to_human_size blob.byte_size %></span>
<% end %>
</figcaption>
</figure>
This is application.scss
#import 'bootstrap';
#import "custom";
#import "mystyles";
#import 'actiontext.css';
When I inspect the browser page, taken from the app on heroku:
I can confirm that the contents of "url=", when open in a browser tab, displays the image that is part of the rich_content
But, as shown in the screen image, I get status 404 errors
browser inspect

How to make the images from the bootstrap cards clickable on your index pages?

I was using the Bootstrap cards for my index page for my app but wasn't really liking the event details button, I feel it's more modern to just click on the image.
I found SOF articles pointing to how to use clickable images if the image was local and/or hosted somewhere online, but nothing that pointed to using images that are already a part of your database.
The code I had before was:
<% if artist.avatar.attached? %>
<%= image_tag artist.avatar, class: "even-size-artist" %>
<% else %>
<img src=<%= "https://dancewise.s3.amazonaws.com/misc-images/Blank+Avatar.png" %> class="even-size-artist">
<% end %>
The code that I found that will give you the option to have the top image clickable to the appropriate record is as follows:
<% if artist.avatar.attached? %>
<%= link_to image_tag(artist.avatar, class: "card-img-top artist-img").html_safe, artist %>
<% else %>
<img src=<%= "https://dancewise.s3.amazonaws.com/misc-images/Blank+Avatar.png" %> class="card-img-top artist-img">
<% end %>
Hopefully, this helps someone else out there learning RoR!

Rails 5 each loop activestorage files displays files attributes

I updated Rails to 5.2 version with ActiveStorage
Everything works fine, but when I perform a loop through my model attached files in order to display them as bootstrap cards, I see my files attributes on the page.
I don't want it to display anything.
How can I prevent that?
<strong style="margin: 10px">Files :</strong>
<div class="card-columns">
<%= #mymodel.files.each do |file| %>
<% end %>
</div>
what it makes on my page
Remove the = from your loop header...
Instead of
<%= #mymodel.files.each do |file| %>
Use
<% #mymodel.files.each do |file| %>
Checkout what the difference is

Form_for in a loop

I'm sure I'm missing something simple, but I'm generating a list of projects each with a form to add a new audition to that particular project. The form isn't being generated properly.
The view:
<% #projects.group_by(&:aasm_state).each do |state, projects| %>
<div class="project-column small-order-<%= state_priority(state) %>">
<div class="project-state spaced-out-2 project-<%= state %>"><%= state.titleize %></div>
<% for project in projects %>
<%= render partial: 'auditions/partials/dash_project_audition_form', locals: {project: project} %>
<% end %>
<% end %>
This is the code in the partial:
<button data-click="new-project-audition">+ Audition for <%= project.title %></button>
<%= form_for([project, #audition]) do |f| %>
... SNIP....
<% end %>
Everything works except the form. Each form generated contains the ID for the last record in #projects. So, for example the last project is ID 19, no matter which project form I click on, they're all for 19. The problem isn't with the partial, it's with something I'm doing in form_for, because + Audition for <%= project.title %>, which is inside the partial, generates the correct title.
UPDATE:
I restarted the server and form_for is now generating the right path, with the right ID.
I have this line also inside the form_for loop, and it's still wrong.
<h3 class="section-head">New Audition for <%= project.title %></h3>
It's generating the title for the last project in the whole loop.
UPDATE 2: Stopped working on a page reload, so... not even sure what to say now.
when you do form_for([project, #audition]), it generates a form for
projects:project_id\auditions:audition_id
so the id that you see in all forms may belong to the #audition variable. Where do you initialize it ?
It should be initialized ideally on a per project basis, right ? Let me know if this helps in any way.

Caching doesn't work Rails 4.0

I'm following the 'Agile Web Development with Rails 4' book and I'm stuck at page 105 about the caching.
I have the following code in index.html.erb
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<h1>Your Pragmatic Catalog</h1>
<% cache ['store', Product.latest] do %>
<% #products.each do |product| %>
<% cache ['entry', product] do %>
<div class="entry">
<%= image_tag(product.image_url) %>
<h3><%= product.title %></h3>
<%= sanitize(product.description) %>
<div class="price_line">
<span class="price"><%= number_to_currency(product.price) %></span>
</div>
</div>
<% end %>
<% end %>
<% end %>
The first doubt is the following:
1) What does cache ['store', Product.latest] exactly do? It creates a cache, available from all StoreController actions, named "store" and associates the cache with the Product.latest: why should I do the last thing? Why do I need to associate my cache to a Product.latest?
Always at the same page the book says: "As far as verifying that this works, unfortunately there isn't much to see. If you go to that page, you should see nothing change, which in fact is the point! The best you can do is to make a change to the template anywhere inside the cache block without updating any product and verifying that you do not see that update because the cached version of the page has not been updated".
So I tried something like this:
<% cache ['store', Product.latest] do %>
"hello"
........
........
<% end %>
But I still get this update, the page shows me the "hello" string, why is it so? Shouldn't I see it?
P.S. Obviously I edited my config/environments/development.rb and restarted the server
Start your application on production mode and you will see that caching works.
What did you edited in development.rb? Please compare it to production.rb and copy-paste all caching configuration properties.
I had the same "problem", and I think what the book is saying is not correct. As far as I can tell, the reason why you see the changes on the website, even though caching is active, is that Rails 4 uses cache digests. For more information, check out:
http://blog.remarkablelabs.com/2012/12/russian-doll-caching-cache-digests-rails-4-countdown-to-2013
When you changed config.cache_classes to false, you just told Rails to not automatically regenerate views with each request. That's why you stopped seeing changes, it's not because "caching" was working correctly.

Resources