diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index fa2d331..507a19a 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -39,10 +39,15 @@ def new def create @product = current_user.products.new(product_params) + if params[:changed_form] + render "create" and return + end + if @product.save redirect_to @product, notice: 'Product was successfully created.' else - render :new, status: 422 + render "create" + # render :new, status: 422 end end @@ -50,11 +55,19 @@ def edit end def update - if @product.update(product_params) + + @product.assign_attributes(product_params) + + if params[:changed_form] + render "update" and return + end + + if @product.save redirect_to @product, notice: 'Product was successfully updated.' else Rails.logger.error("AAA #{@product.errors.full_messages}") - render :edit, status: 422 + # render :edit, status: 422 + render "update" end end diff --git a/app/javascript/controllers/simple_editor_controller.jsx b/app/javascript/controllers/simple_editor_controller.jsx index e0bc444..34d7007 100644 --- a/app/javascript/controllers/simple_editor_controller.jsx +++ b/app/javascript/controllers/simple_editor_controller.jsx @@ -119,9 +119,12 @@ export default class extends Controller { this.editor.commands.toggleItalic() } - toggleHeading(e){ - e.preventDefault() - this.editor.commands.toggleHeading({ level: parseInt(e.currentTarget.dataset.level) }) + toggleHeading(e) { + // Check if the event is from a click or a specific key press on the button + if (e.type === 'click') { // || (e.type === 'keydown' && e.key === 'Enter' && e.target.matches('[data-action="toggleHeading"]'))) { + e.preventDefault(); + this.editor.commands.toggleHeading({ level: parseInt(e.currentTarget.dataset.level) }); + } } toggleOrderedList(e){ diff --git a/app/javascript/controllers/submit_on_change_controller.js b/app/javascript/controllers/submit_on_change_controller.js index bddd61e..4774c6d 100644 --- a/app/javascript/controllers/submit_on_change_controller.js +++ b/app/javascript/controllers/submit_on_change_controller.js @@ -1,7 +1,21 @@ -import { Controller } from "@hotwired/stimulus" +import { Controller } from "@hotwired/stimulus"; export default class extends Controller { - handleChange(event) { + connect() { + console.log("SubmitOnChangeController connected"); + } + + submitForm(event) { + // Prevent the default behavior if needed + // event.preventDefault(); + + let hiddenInput = document.createElement("input"); + hiddenInput.type = "hidden"; + hiddenInput.name = "changed_form"; + hiddenInput.value = "true"; + this.element.appendChild(hiddenInput); + + // Submit the form this.element.requestSubmit(); } } \ No newline at end of file diff --git a/app/views/products/_form.erb b/app/views/products/_form.erb index 0da4857..65724f9 100644 --- a/app/views/products/_form.erb +++ b/app/views/products/_form.erb @@ -9,14 +9,26 @@ <% end %> - <%= form_with(model: product, local: true) do |form| %> - - <%= form.object.errors.full_messages.join(" ") %> + <%= form_with(model: product, local: true, data: { + controller: "submit-on-change" + }) do |form| %> + + <% if form.object.errors.any? %> +
Errors Found
+ <% # form.object.errors.full_messages.join(" ") %> +