ruby 真实的广播不工作在rails 7

4jb9z9bj  于 2022-11-04  发布在  Ruby
关注(0)|答案(1)|浏览(130)

我在rails模型中添加了广播,请看下面的文件。

注解模型

class Comment < ApplicationRecord

  validates :body, presence: true
  belongs_to :post
  after_create_commit { broadcast_append_to "comments_#{self.post_id}", partial: "posts/comment" }

end

发布模型

class Post < ApplicationRecord

  has_many :comments, dependent: :destroy

end

应用程序/视图/帖子/索引.html.erb

<h1 class="text-center m-4 text-success">Posts</h1>
<div id="posts">
    <div class="row">
        <% @posts.each do |post| %>
            <%= render post %>
        <% end %>
    </div>
</div>

应用程序/视图/帖子/_post.html.erb

<div class="col-lg-4 mb-2">
    <div class="card border-0" style="width: 18rem;">
        <div class="card-body">
            <p>
                <span class="comment">
                    <%= link_to fa_icon("comment"),"#", data: { action: "click->like#comment",comment_url: new_post_comment_path(post.id) } %>
                </span>
            </p>
            <div id="add_comment_<%= post.id %>"></div>
            <h5 class="card-title"><%= post.title %></h5>
            <p class="card-text"><%= post.description %></p>
            <strong>Comments</strong>
            <%= turbo_stream_from "comments_#{post.id}" %>
            <%= turbo_frame_tag "comments_#{post.id}" do %>
                <div id="add_comments_<%= post.id %>">
                    <% post.comments.each do |comment_post| %>
                        <%= render partial: "posts/comment", locals: { comment: comment_post } %>
                    <% end %>
                </div>
            <% end %>
        </div>
    </div>
</div>

应用程序/浏览次数/帖子/_评论.html.erb

<p id="comment_<%= comment.id %>" data-controller="comment"><%= comment.body %>
    &nbsp;
    <span>
        <%= link_to "edit",post_comment_edit_path(comment.post,comment), data: { turbo_method: :post }  %>
    </span>
    &nbsp;
    <span><%= link_to "delete", post_comment_path(comment.post,comment), data: { turbo_method: :delete } %></span>
    &nbsp;
    <span><%= commentUpdatedAt(comment) %> ago</span>
</p>

我已经检查了binding.pry,在评论模型中创建评论时没有得到post_id,所以目标不同。
请帮帮我

6pp0gazn

6pp0gazn1#

我已经找到了上面的答案,

应用程序/模型/注解.rb文件

class Comment < ApplicationRecord

  validates :body, presence: true
  belongs_to :post
  belongs_to :user

  after_create_commit :set_comment
  after_update_commit :update_comment
  after_destroy_commit :destroy_comment

  private

  def set_comment
    broadcast_append_to "add_comments_#{self.post.id}", partial: "posts/comment", target: "add_comments_#{self.post.id}"
  end

  def update_comment
    broadcast_update_to "comment_#{self.id}", partial: "posts/comment", target: "comment_#{self.id}"
  end

  def destroy_comment
    broadcast_remove_to "comment_#{self.id}"
  end

end

应用程序/视图/帖子/_post.html.erb文件

<strong>Comments</strong>
            <%= turbo_stream_from "add_comments_#{post.id}" %>
            <div id="add_comments_<%= post.id %>">
                <% post.comments.each do |comment_post| %>
                    <%= render partial: "posts/comment", locals: { comment: comment_post } %>
                <% end %>
            </div>

应用程序/视图/帖子/_评论.html.erb文件

<%= turbo_stream_from "comment_#{comment.id}" %>
<p id="comment_<%= comment.id %>" data-controller="comment"><%= comment.body %>
    &nbsp;
    <span>
        <%= link_to "edit",post_comment_edit_path(comment.post,comment), data: { turbo_method: :post }  %>
    </span>
    &nbsp;
    <span><%= link_to "delete", post_comment_path(comment.post,comment), data: { turbo_method: :delete } %></span>
    &nbsp;
    <span>
        <%= link_to "reply",replyComment_post_comment_index_path(comment.post), data: { turbo_method: :post }  %>
    </span>&nbsp;
    <span><%= commentUpdatedAt(comment) %> ago</span>
</p>

解决方案正如我所期望的那样运行良好。

相关问题