Search code examples
ruby-on-railsrubyruby-on-rails-6.1hotwire-railsturbo

how to redirect a form within turbo_frame?


I need to add a button that persists the data of the following form but that when pressing it resets the view and stays in it, without redirecting to the index of the controller, I have tried an ajax call but I don't know why it didn't work for me. How can I give a solution and add the button to save and continue?

<%= turbo_frame_tag dom_id(taxonomy) do %>
  <%= form_with(model: [:admin, taxonomy], id: dom_id(taxonomy)) do |form| %>
<div class="box">
  <div class="field is-horizontal">
    <div class="field-body">
      <div class="field">
        <div class="control is-expanded is-relative">
          <%= form.text_field :name, placeholder: true, class: "input mt-5" %>
          <%= form.label :name, class: "label-hidden required" %>
        </div>
      </div>

      <div class="field is-narrow">
        <div class="control mt-5">
          <%= form.submit nil, class: "button is-primary" %>
          <%= link_to "#{ t '.cancel'}", admin_taxonomies_path, class: "button is-light" %>
        </div>
      </div>
    </div>
  </div>
</div>
<% end %>
<% end %>

Solution

  • You need to add a target="_top" attribute to the frame: turbo_frame_tag dom_id(taxonomy), target: :_top Otherwise Turbo tries to replace the current frame containing the form.