Forcing a page reload on turbo_frame when form is submitted with correct data

I have been working with turbo frames to make login modal and I found a situation where I need to disable turbo functionality of page updating without a refresh. The problem is that I want to refresh the page only if the credentials are correct - if I set data-turbo: false, that makes it refresh the page on failure and visit login route, which I don’t want to. So, what is the best approach in such situation? Can I somewhat easily override turbo behaviour to react to rails successfully validating the data sent by a form? How should I do so?

1 Like

We’re trying to find a good solution to this as well.

In that case, I might share a solution here if I come up with something.

Okay, I have an update to my situation.

@Josh_Marchello While I don’t think that is the best solution, that was the fastest and the easiest one I came up with.

/* app/assets/javascript/controllers/modal_controller.js */
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
        this.element.addEventListener("turbo:before-fetch-response", (event) => {
            const response = event.detail.fetchResponse.response
            if (response.status == "200" && response.url.includes("login")) {
    /* And the rest of modal controller code */

So what I basically did is that I added EventListener for turbo:before-fetch-response on form element that has my modal controller, since it’s one of two turbo events that retrieve response status data (the other is turbo:submit-end). Then I check for response status code (Url is checked only because the stimulus controller is shared on two form elements, that can be easily skipped if controller is included only on those elements that should trigger page refresh). After that I use location.reload() to refresh the page.

That isn’t ideal solution, since I would prefer to actually turn off turbo behaviour but it works and should suffice for now. I might lean more into source code itself to find how exactly turbo drive is being triggered later on.