uploadcare / uploadcare-rails

Rails API client that handles uploads and further operations with files by wrapping Uploadcare Upload and REST APIs.
https://uploadcare.com
MIT License
58 stars 70 forks source link

Submit form after clicked on "add" button #104

Closed GuillaumeOcculy closed 2 years ago

GuillaumeOcculy commented 2 years ago

rails (7.0.2.3) uploadcare-rails (2.1.0) importmap-rails (1.0.1)

Is there a way to submit the form after clicked on "add" button ?

Screenshot 2022-04-26 at 00 36 42

Right now I have to click on "add" and then click on my :submit button

# app/models/uploadcare_document.rb
class UploadcareDocument < ApplicationRecord
  mount_uploadcare_file_group :attachments
end

# app/views/uploadcare_documents/_form.html.erb
<%= simple_form_for uploadcare_document do |f| %>
  <%= uploadcare_uploader_field :uploadcare_document, :attachments %>
  <%= f.button  :submit %>
<% end %>

https://user-images.githubusercontent.com/3475334/165186709-1c6468f7-2c37-4971-9fbf-6f73d5d986a9.mp4

optlsnd commented 2 years ago

Hi @GuillaumeOcculy,

This is what you can achieve with some JS.

const uploader = uploadcare.Widget("[role=uploadcare-uploader]");
const form = document.getElementById("your-form-identifier");
uploader.onUploadComplete(function () {
  form.submit();
});

Instead of submitting the form right away after a click on the Add button, it's better to wait until all files are uploaded. If you still want to submit the form immediately, replace the onUploadComplete method with onChange.

GuillaumeOcculy commented 2 years ago

Thanks 🙏