Submit image when selected

This could have been done with a tiny amount of inline JS, but I went this way in case I needed any special logic for UJS. It turns out we don't.. but it still looks a bit cleaner this way.
This commit is contained in:
David Cook
2024-01-11 15:00:15 +11:00
parent 6479990f6d
commit 6ed96bae85
2 changed files with 9 additions and 3 deletions

View File

@@ -5,12 +5,11 @@
-# Submit to controller, because StimulusReflex doesn't support file uploads
= form_for [:admin, product, image], url: admin_product_image_path(product, image),
html: { multipart: true } do |f|
html: { multipart: true }, data: { controller: "form" } do |f|
%input{ type: :hidden, name: :return_url, value: return_url}
= f.hidden_field :viewable_id, value: product.id
%p
-# label.button provides a handy shortcut to open the file selector on click. Unfortunately this trick isn't keyboard accessible though..
= f.label :attachment, t(".upload"), class: "button primary icon-upload-alt"
= f.file_field :attachment, style: "display: none"
= f.submit # todo: submit automatically
= f.file_field :attachment, style: "display: none", "data-action": "change->form#submit"

View File

@@ -0,0 +1,7 @@
import { Controller } from "stimulus";
export default class FormController extends Controller {
submit() {
this.element.submit();
}
}