0

I have a model subscription_tier with show and edit actions with corresponding views. I've wrapped each of these with a turbo frame

<%= turbo_frame_tag subscription_tier do %>

When I edit an existing subscription tier and save it, the turbo frame refreshes and shows my saved tier, but I'm unable to replicate this for creating new tiers.

My new tier frame:

<turbo-frame id="new_tier">
      <%= link_to "Add Tier", new_create_subscription_tier_path(sub_type: "Free"), class: "btn btn-primary mb-3 fs-6"
      %>
 </turbo-frame>

and in new.html.erb


    <turbo-frame id="new_tier">
      <%= render partial: "create/subscription_tiers/edit", locals: {
        subscription_tier: @subscription_tier
      } %>
    </turbo-frame>

Clicking Add Tier button successfully renders the form and I can save the object, but on save the turbo frame is destroyed with the error Response has no matching <turbo-frame id="new_tier"> element

I know this is because my show partial is wrapped with the <%= turbo_frame_tag subscription_tier do %>, but I don't know how to reconcile this.

sasav
  • 1
  • 1
  • 2

2 Answers2

2

I believe you need to add a data tag to your new link i.e.

<%= link_to "new subscription", subscription_path, data: { turbo_frame: dom_id(Subscription.new) } %>

On the same page as this link ^^^^ you need to add

<%= turbo_frame_tag Subscription.new %>

I believe this is what your trying to accomplish

There is a fantastic tutorial that goes over all this stuff that is free here

Chrismisballs
  • 167
  • 12
0

Perhaps the OP figured this out, but for anyone looking for a possible solution, you could always set the turbo frame ID manually, for example:

<!-- new.html.erb, edit.html.erb -->
<%= turbo_frame_tag 'subscription_tier_frame' do %>
  <%-- form for subscription tier -->
<% end %>

and

<!-- show.html.erb -->
<%= turbo_frame_tag 'subscription_tier_frame' do %>
  <%-- rendered subscription tier -->
<% end %>

That'll keep things in the same turbo frame.

lshepstone
  • 136
  • 9