How to prevent a forms default behaviour of reloading the page after form submission

Hello there,

I’m trying to event.preventDefault() on a specific form after form submission, I have tried executing javascript code from my create.js.erb file for the create action but this doesn’t work. None of the code in there get’s executed.

I’m using the form_with tag to render my form and I have tried adding the local: true, remote: true (I read somewhere that this was unnecessary because it comes with this property remote: true by default) property to it but none worked.

Here’s how my create action looks like:

 def create
    @user =
    respond_to do |format|
        format.json { render json: @user, status: :created, location: @user }
        format.json { render json: "cool", status: :unprocessable_entity }

Thank you for your assistance, any help is much appreciated!