I have a new user form, which renders with a different layout than the normal user show view. I need to either redirect or somehow swap out the full page on the form submission, but I want to keep the turbostream functionality for rendering errors.
So far, redirecting to show fails to render the layout and fails to update the page.
Is there a standard way to do this?
If this form is inside of a turbo-frame you can set the target attribute to
“_top” this means anything inside would navigate the whole page than when you redirect it should navigate the whole page.
Wouldn’t that break the normal behavior for when errors are returned? Also, I can’t get any views to be rendered with the full layout.
For the errors to still be rendered without leaving the page you could use a turbo_stream response from the controller to replace the form. about the views not being rendered with the layout I’m not sure, where is it happening exactly?
_top indeed ruins the benefit of putting the form in a
turbo_frame. Personally I’ve found that it also just doesn’t work with redirect responses and I’m confused as to why I keep seeing it recommended.
There’s a bunch of posts expressing confusion about this exact issue. See:
- Turbo and form redirects (again!)
- Form redirects not working as expected
- Break out of a frame during form redirect - #6 by adamsestra
- Redirect to new page on successful form submission, rerender otherwise · Issue #138 · hotwired/turbo · GitHub
The form replacement is great for errors, but when the form succeeds and gets a redirect response, Turbo, by design, doesn’t follow it. Confusing, right?
My recommendation is to disable Turbo on any form that has a redirect response. Seems crazy but the alternative of using a hack to call
Turbo.visit after Turbo doesn’t render the redirect is worse IMO. The only forms where I have Turbo enabled are ones where the success response can just replace the form with a success message. For example when the user has to go click a confirm link in their email.