Rails 7 Turbo-Stream广播到特定频道不起作用

发布于 2025-01-30 23:03:30 字数 2228 浏览 3 评论 0原文

我有一个帖子模型和评论,以及帖子has_many的评论,我想在您在特定帖子中时实际更新评论,含义是,只有在多个用户在同一帖子中时,现场更新的评论才能可见:在帖子/6中

,我有:

<div>
    <p>
      <strong>Title:</strong>
      <%= post.title %>
    </p>

    <p>
      <strong>Content:</strong>
      <%= post.content %>
    </p>

    <p>
      <strong>User:</strong>
      <%= post.user.username %>
    </p>
  </div>

  <hr/>

  <%= form_for [post, Comment.new] do |f| %>
    Body: <%= f.text_area :body, rows: 5, cols: 20 %>
    <%= f.submit "Submit" %>
  <% end %>

  <hr/>
  <h1>Comments:</h1>
  <%= turbo_stream_from post %>
  <%= turbo_frame_tag post do %>
    <div id="<%= dom_id(post) %>">
      <% post.comments.includes(:user).each do |comment| %>
        <%= render comment %>
      <% end %>
    </div>
  <% end %>

在评论中#创建操作我有:

    post = Post.find(params[:post_id])
    @comment = post.comments.new(comment_params)
    @comment.user_id = current_user.id

    respond_to do |format|
      if @comment.save
        
        format.turbo_stream { render turbo_stream: turbo_stream.prepend("post_#{post.id}", partial: "comments/comment", locals: { comment: @comment }) }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end

在评论模型中我有:

after_create_commit { 
    broadcast_prepend_to(target: "post_#{self.post.id}")
    
  }

在终端我可以看到

[ActionCable] Broadcasting to : "<turbo-stream action=\"prepend\" target=\"post_6\"><template>  <div>\n    <p>\n      <strong>Title:</strong>\n      adsd\n    </p>\n\n    <p>\n      <strong>Content:</strong>\n      Asda\n    </p>\n\n    <p>\n      <strong>User:</strong>\n      Super Saiyan Vegeta\n    </p>\n  </div>\n\n  <hr/>\...

哪个是正确的,但是特定帖子中仍未更新的评论列表,我确实看到我在视图中有ID =“ post_6” ...有人可以告诉我为什么吗?谢谢。

I have a Post model and Comment, and Post has_many Comment and I wanna real-timely update the comment when you are in a specific post, meaning, the comments live-updating should only visible when multiple users are in the same post, for example: in posts/6

For View I have:

<div>
    <p>
      <strong>Title:</strong>
      <%= post.title %>
    </p>

    <p>
      <strong>Content:</strong>
      <%= post.content %>
    </p>

    <p>
      <strong>User:</strong>
      <%= post.user.username %>
    </p>
  </div>

  <hr/>

  <%= form_for [post, Comment.new] do |f| %>
    Body: <%= f.text_area :body, rows: 5, cols: 20 %>
    <%= f.submit "Submit" %>
  <% end %>

  <hr/>
  <h1>Comments:</h1>
  <%= turbo_stream_from post %>
  <%= turbo_frame_tag post do %>
    <div id="<%= dom_id(post) %>">
      <% post.comments.includes(:user).each do |comment| %>
        <%= render comment %>
      <% end %>
    </div>
  <% end %>

In Comment#create action I have:

    post = Post.find(params[:post_id])
    @comment = post.comments.new(comment_params)
    @comment.user_id = current_user.id

    respond_to do |format|
      if @comment.save
        
        format.turbo_stream { render turbo_stream: turbo_stream.prepend("post_#{post.id}", partial: "comments/comment", locals: { comment: @comment }) }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end

In Comment model I have:

after_create_commit { 
    broadcast_prepend_to(target: "post_#{self.post.id}")
    
  }

And in terminal I can see

[ActionCable] Broadcasting to : "<turbo-stream action=\"prepend\" target=\"post_6\"><template>  <div>\n    <p>\n      <strong>Title:</strong>\n      adsd\n    </p>\n\n    <p>\n      <strong>Content:</strong>\n      Asda\n    </p>\n\n    <p>\n      <strong>User:</strong>\n      Super Saiyan Vegeta\n    </p>\n  </div>\n\n  <hr/>\...

Which is correct, but the comments list in a specific post still not updating, and I do see I have id="post_6" in the view...can someone tell me why? Thanks.

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

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

发布评论

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

评论(1

泡沫很甜 2025-02-06 23:03:30

尝试在控制器中更改

<div id="<%= dom_id(post) %>">

<div id="post_<%= post.id %>">

使用此类方法,

if @comment.save
  @comment.broadcast_prepend_to to post, target: "post_#{post.id}", partial: "comments/comment", locals: { comment: @comment }
end

而不是从模型中广播

Try to change

<div id="<%= dom_id(post) %>">

to

<div id="post_<%= post.id %>">

And use such method in controller

if @comment.save
  @comment.broadcast_prepend_to to post, target: "post_#{post.id}", partial: "comments/comment", locals: { comment: @comment }
end

instead of broadcast from model

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