[Ruby On Rails] Causes and remedies for ActionView :: Template :: Error (ActiveStorage :: InvariableError) (hypothesis / verification)

It is a memorandum. We hypothesized and verified the cause of ActionView :: Template :: Error (ActiveStorage :: InvariableError) and how to deal with it.

Error background / background

I decided to check the operation of the deployed application. This time, I want to simply manually check whether the image has been uploaded, so I decided to check the operation without performing unit tests or integration tests.

This time, I asked my friends around me to cooperate in the operation check test. As for the cooperation, I asked to attach the file to the form where the image can be posted and check whether the posted file can be displayed in the view.

error contents

I received a report from a friend who is a collaborator that I attached an image file, so I checked the details screen of the deployed application. Then, "We're sorry but something went wrong" was displayed.

First, I checked the log and its error contents in the terminal. (This time heroku)

% heroku logs --tail

(Omission)

ActionView::Template::Error (ActiveStorage::InvariableError)
    34: <div>
    35:image
    36: </div>
    37: <%= image_tag @question.image.variant(resize: '200x200'), class: 'question-image' if @question.image.attached? %>
    38: <% end %>
    39:
    40: <%= render 'shared/error_messages', model: @comment %>
app/views/questions/show.html.erb:37

What I looked up

I checked the article about the error content. The key to resolving this error was "use the variable? Method to distinguish whether a blob is a variable". The variable mentioned here is the attached image, but it turned out that the key is whether the correct variable (image) is given.

"What is variable? In the first place?"

So, when I looked it up on the net, it was used when using variable? To make it possible to call the image only when it was True. Then, I found that by combining it with the ActiveStorage.variable_content_types method, it limits the attachment of images with only the specified extension.

hypothesis

In the first place, the file posted by a friend is not an image file, or an image with an extension supported by active_storage. I made the hypothesis.

What went

I asked a friend about the possibility of posting something other than an image. Then, the answer was "I attached the file without checking it with Texto from the files with both images and files with other extensions". In other words, it is highly possible that something other than the image is attached.

Based on what I heard from my friends and my hypothesis, I tried to see if I could attach and save a file with the wrong extension, and if the details page would result in an error.

result

As with my friends, I was able to save data with a completely unrelated extension. Then, when I moved to the details page, the message "We're sorry but something went wrong" was displayed as well.

In other words, it turned out that "a file with the correct extension was not sent".

In the future, use the ActiveStorage.variable_content_types method etc. so that saving other than a specific extension will be rejected so that an error like this one does not occur.

Supplement

During the testing process, there were cases where uploading could not be done if the image extension was .heic (in some cases, the cause is unknown). As described in the second article of the reference article, it seems that files with the .heic extension are not currently supported, so it may be better to avoid it and to repel only good or heic. I don't know.

Reading articles in English is pretty important

Reference article

https://edgeapi.rubyonrails.org/classes/ActiveStorage/InvariableError.html https://stackoverflow.com/questions/60686249/activestorageinvariableerror-in-homeindex https://qiita.com/xusaku_/items/36a61e35b6cd863bbf9d

Recommended Posts

[Ruby On Rails] Causes and remedies for ActionView :: Template :: Error (ActiveStorage :: InvariableError) (hypothesis / verification)
Explanation of Ruby on rails for beginners ⑤ ~ Edit and delete database ~
Create a development environment for Ruby 3.0.0 and Rails 6.1.0 on Ubuntu 20.04.1 LTS
[Ruby on Rails] About bundler (for beginners)
[Ruby / Rails] Mechanism for retrying Thread Error
Explanation of Ruby on rails for beginners ①
[Ruby on Rails] Add and delete tags and display (success / error) messages using ajax.
Validation settings for Ruby on Rails login function
[Beginner Procedure Manual 2] Ruby on Rails: Rails template creation
[Ruby on Rails] How to display error messages
[Ruby on Rails] Select2 introduction memo for Webpacker
Ruby on Rails ~ Basics of MVC and Router ~
[Rails] Procedure for linking databases with Ruby On Rails
[Ruby on Rails] Individual display of error messages
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
[Ruby on Rails Tutorial] Error in the test in Chapter 3
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
Explanation of Ruby on rails for beginners ② ~ Creating links ~
(Ruby on Rails6) How to create models and tables
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
Causes and remedies for the exception "ServletException: Error instantiating servlet class" when deploying Java applications