如何使用 Turbo 框架将对象创建表单替换为对象显示部分?

发布于 2025-01-09 11:33:46 字数 995 浏览 4 评论 0原文

我有一个模型 subscription_tier ,其中包含 showedit 操作以及相应的视图。我已经用 Turbo 框架包裹了每一个

<%= Turbo_frame_tag subscription_tier do %>

当我编辑现有订阅层并保存它时,turbo 框架会刷新并显示我保存的层,但我无法复制此内容以创建新层。

我的新层框架:

<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>

new.html.erb 中,


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

单击“添加层”按钮成功呈现表单,我可以保存对象,但在保存时,turbo 框架被破坏,并出现错误 Response has没有匹配的element

我知道这是因为我的 show 部分是用 <%= Turbo_frame_tag subscription_tier do %> 包装的,但我不知道如何调和这一点。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月亮坠入山谷 2025-01-16 11:33:46

我相信您需要在新链接中添加一个数据标签,即

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

在与此链接相同的页面上 ^^^^ 您需要添加

<%= turbo_frame_tag Subscription.new %>

我相信这就是您想要完成的任务

有一个很棒的教程,涵盖了所有这些内容此处免费

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

夏雨凉 2025-01-16 11:33:46

也许OP已经解决了这个问题,但是对于任何寻找可能解决方案的人来说,您总是可以手动设置turbo帧ID,例如:

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

并且

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

这会将事物保留在同一个turbo帧中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文