Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting filename before saving in ActiveStorage - Rails 5.2.1

I am trying to get the name of the file uploaded by a user before ActiveStorage goes on to save it. The form is generated using form_with and is shown below:

<%= form_with model: upload do |form| %>
  <div class="">
    <%= form.file_field :files, multiple: true, direct_upload: true, required: true %>
    <%= form.label :files, '', class: 'icon ion-ios-cloud-upload' do %>
      <span>click the icon to select files</span>
    <% end %>

    <div class="actions">
      <%= form.submit "Upload", class: "btn btn-primary" %>
    </div>
 </div>
<% end %>

I have tried accessing params[:upload][files] and calling .original_filename on it as described here but I get the error NoMethodError: undefined method `original_filename' for #<String:0x007fac77fd18c8>.

The file does come back as a string when I inspect the params, so how do I get the filename or how do I get original_filename to work?

like image 783
troy34 Avatar asked Dec 08 '22 13:12

troy34


1 Answers

I was finally able to get the file name by doing file.blob.filename after the file had been attached.

like image 174
troy34 Avatar answered Jan 10 '23 02:01

troy34