在带有Turbo的Rails 7中,Broadcast_prepend_later_to正在工作,但对于页面重定向太快

发布于 2025-02-03 04:33:21 字数 1343 浏览 2 评论 0原文

我有一个带有“锻炼”表格的Rails 7应用程序。提交表格后,我将有两个执行的回调。

首先是锻炼模型上的commit 之后的。这会找到/更新或创建锻炼记录。第二个是在锻炼上创建commit回调之后,以广播broadcast_prepend_later_to显示所有显示所有锻炼>锻炼的页面。

当前,如果我打开两个浏览器窗口,并且将广播定位的页面之一放置在表单提交时会更新。我用来键入表单并提交的另一个浏览器窗口,将另一个浏览器窗口打开的同一页面retirects_to。唯一的问题是新记录并不总是显示。我猜想该页面在新唱片存在之前正在重新加载,但是在广播已经熄灭之后。

我的问题是如何处理此问题,以便在提交表格并将其重定向到页面后可以看到新记录?

锻炼。RB

class Workout < ApplicationRecord
  include Destroyable
  
  has_one :workout_summary, dependent: :delete

  after_commit :recalculate_workout_summary

  def recalculate_workout_summary
    WorkoutSummaryJob.perform_later self
  end

end

锻炼_summary.rb

class WorkoutSummary < ApplicationRecord
  include MeasurableCalculable, ActionView::RecordIdentifier

  belongs_to :workout

  after_create_commit :broadcast_later

  delegate :date, to: :workout, allow_nil: true
  delegate :player, to: :workout

  private
    
  def broadcast_later
    broadcast_prepend_later_to [player, :workout_summaries], target: "#{dom_id(player)}_workout_summaries", partial: "players/workouts/workout_summary", locals: { workout_summary: self }
  end

  def summarize
    recalculate_measurables(workout.measurables)
    self.save
  end

end

I have a Rails 7 application with a "Workout" form. Once I submit the form, I have two callbacks that execute.

The first is the after commit on the Workout model. This finds/updates or creates a WorkoutSummary record. The second is the after create commit callback on WorkoutSummary to broadcast an broadcast_prepend_later_to a page that displays all workout_summary records.

Currently, if I open up two browser windows and I put one of the page the broadcast is targeting, it updates when the form submits. The other browser window that I use to type in the form and submit, redirects_to the same page the other browser window has open. The only problem is the new record doesn't always show up. I'm guessing the page is reloading before the new record exists, but after the broadcast has already gone out.

My question is how should I handle this so that I can see the new record after submitting the form and being redirected to the page?

workout.rb

class Workout < ApplicationRecord
  include Destroyable
  
  has_one :workout_summary, dependent: :delete

  after_commit :recalculate_workout_summary

  def recalculate_workout_summary
    WorkoutSummaryJob.perform_later self
  end

end

workout_summary.rb

class WorkoutSummary < ApplicationRecord
  include MeasurableCalculable, ActionView::RecordIdentifier

  belongs_to :workout

  after_create_commit :broadcast_later

  delegate :date, to: :workout, allow_nil: true
  delegate :player, to: :workout

  private
    
  def broadcast_later
    broadcast_prepend_later_to [player, :workout_summaries], target: "#{dom_id(player)}_workout_summaries", partial: "players/workouts/workout_summary", locals: { workout_summary: self }
  end

  def summarize
    recalculate_measurables(workout.measurables)
    self.save
  end

end

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文