Hotwire Discussion

[Turbo] Redirect after form submit?

When I submit a form inside a frame, if the form has an error it’s OK, the frame is updated and all errors are displayed.

But when the controller return a redirect, how can I redirect the user or replace the “new_registration” frame by the content of the redirection response please?

<%= turbo_frame_tag 'new_registration' do %>
    <%= form_with model: @user, url: sign_up_path, local: true do |form| %>
          ...
    <% end %>
<% end %>

Responses:

Thanks.

You have two options:

  1. If you want only that part of the page to be updated, you need to wrap the response in a turbo frame with a corresponding ID (turbo_frame_tag 'new_registration').
  2. If you want a full-page redirect (as in, the entire page gets replaced with the redirected page), add a target="_top" attribute to the frame (turbo_frame_tag 'new_registration', target: :_top).

https://turbo.hotwire.dev/handbook/frames

It is like above. I want to discuss this with devs of hotwire at Redirect to new page on successful form submission, rerender otherwise · Issue #138 · hotwired/turbo · GitHub.