active storage prevent client reupload file when form error validation

Viewed 136

I have a form with many inputs, and one is a kbis, an file input.

My model:

class Company < ApplicationRecord
  validates :name, presence: true
  validate :presence_kbis

  has_one_attached :kbis

  private 

  def presence_kbis
    errors.add(:kbis, :blank) unless kbis.attached?
  end
end

I created a classic form, and one input is:

<%= f.input :kbis %>

My problem is if an other field contains an error, I lost the file, and I ask again to the user to select again a file. I understood that for security reasons, the navigator can not select the file. But the file is already uploaded to the rails server.

My question:

  • There is a way to use the file already uploded? In CarrierWawe we could do something like this:
<%= f.file_field :avatar %>
<%= f.hidden_field :avatar_cache %>
  • I tried to do this:
<%= f.hidden_field :kbis, value: f.object.kbis.signed_id if f.object.kbis.attached?  %>
<%= f.input :kbis %>

but when I submit I have in exception: ActiveSupport::MessageVerifier::InvalidSignature

  • I think I have correcty understood how data is stored when succesfully, but I am not clear with differents step. It is store before or after validation? Where is the file is validation failed? It is removed after http request? If not when it is remove?

CONTEXT:

  • ruby 3.0.1
  • rails 6.1.3.1
  • environement: development
  • disk storage: local
  • I use simple_form
0 Answers
Related