Access Current_user / current_account上下文中的涡轮流部分?
因此,在我的应用中,我在流中有用户和帐户信息: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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论