Access Current_user / current_account上下文中的涡轮流部分?

发布于 2025-02-07 22:57:04 字数 1040 浏览 0 评论 0原文

因此,在我的应用中,我在流中有用户和帐户信息:web | [ActionCable] [用户8] [帐户3] Turbo :: StreamSchannel Transmitting ...

在此处设置:

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    include SetCurrentRequestDetails

    identified_by :current_user, :current_account, :true_user
    impersonates :user

    delegate :params, :session, to: :request

    def connect
      self.current_user = find_verified_user
      set_request_details
      self.current_account = Current.account

      logger.add_tags "ActionCable", "User #{current_user.id}", "Account #{current_account.id}"
    end
end

示例将是基于涡轮增压的评论系统。将新评论推向给定页面上的所有用户 - 原始海报和其他用户。海报可以编辑 /破坏(通过专家授权),而其他用户可以查看(即隐藏 /禁用操作)。

当您通过Turbo附加部分时,您不能只调用current_user等。您可以将current_user传递到广播中的技巧,但这只是海报用户ID。

如果日志能够访问用户,并且帐户可以从部分broadcast_append内部使用?

broadcast_append_to [commentable, :comments], target: "comments", partial: "comments/comment", locals: { user: user }

So in my app I have the user and account info in the stream: web | [ActionCable] [User 8] [Account 3] Turbo::StreamsChannel transmitting...

Set here:

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    include SetCurrentRequestDetails

    identified_by :current_user, :current_account, :true_user
    impersonates :user

    delegate :params, :session, to: :request

    def connect
      self.current_user = find_verified_user
      set_request_details
      self.current_account = Current.account

      logger.add_tags "ActionCable", "User #{current_user.id}", "Account #{current_account.id}"
    end
end

The example would be a turbo based comments system. New comments are pushed to all users on a given page - the original poster and other users. The poster can edit / destroy (via Pundit authorization) and the other users just view (i.e. hide / disable the actions).

When you append a partial via Turbo you can't just call current_user etc. as that fails. You can do the trick to pass current_user into the broadcast but that is only the poster user ID.

If the logs are able to have access to the user and account can you that from inside the partial broadcast_append?:

broadcast_append_to [commentable, :comments], target: "comments", partial: "comments/comment", locals: { user: user }

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

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

发布评论

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