How to submit a form without reloading turbo_frame_tag

938 Views Asked by At

Using tubro_frame in Rails, how to submit a form which is inside the turbo frame without reloading it?

Sample code:

<%= turbo_frame_tag "sample-frame" do %>
  <%= form_with(model: model, url: sample_p_url, method: :put) do |f| %>
    #other fields here
    <%= f.submit "Save" %>
   <% end %>
<% end %>

Issue: When submitting the form, it also reloads the frame. I tried to supply data: {turbo: true) or data: {remote: true} to form but not working

1

There are 1 best solutions below

2
Ray On

If you want this form to remain on the page and update a different part of the page, then you would need for the form to be outside of the turbo-frame. Then, you can explicitly set the frame that this form controls by setting a data-turbo-frame attribute for the form. Turbo Docs: Targeting Into or Out of a Frame

However, if you wish to allow your form to submit multiple times, I would suggest allowing it to reload as a fresh form on the page, so that you get a fresh authenticity_token.