0

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

aldrien.h
  • 3,437
  • 2
  • 30
  • 52

1 Answers1

0

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.

Ray
  • 366
  • 3
  • 5
  • Hi @Ray - thanks for your answer, i have that set up in other cases where form(hidden) is outside of turbo_frame and manually triggering the submit action via Stimulus script. However this idea is not good option for me since the form needs to be dynamic and part of the table row loop. For a workaround - what i did is to wrap the form by another turbo_frame, what happened is that it only reloads this frame not the parent one :) – aldrien.h Jun 12 '23 at 01:15
  • Take another look at the docs on "Targeting." Can you set `target` to the `id` of the parent frame that you want to have reload? – Ray Jun 22 '23 at 18:57