微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Hotwire turbo 流返回头部 :no_content

如何解决Hotwire turbo 流返回头部 :no_content

我有一个包含帖子和评论的系统,每个 Post has_many Comment。我正在尝试设置 turbostream,以便在您发表评论时立即显示

一切正常,因为数据被持久化到数据库中,但似乎 turbo 流没有正确返回。当我点击“评论”按钮时,没有任何变化,我收到一条 :no_content

CommentsController#create 消息
  ↳ app/controllers/comments_controller.rb:11:in `create'
[ActiveJob] Enqueued Turbo::Streams::ActionbroadcastJob (Job ID: b0be3a08-d7bb-4216-aac5-2f274a22dcbf) to Async(default) with arguments: "Z2lkOi8vY2lhby9Qb3N0LzM",{:action=>:append,:target=>"comments",:locals=>{:comment=>#<GlobalID:0x00007fa94123baf8 @uri=#<URI::GID gid://Ciao/Comment/16>>},:partial=>"comments/comment"}
[ActiveJob] Enqueued Turbo::Streams::ActionbroadcastJob (Job ID: 382e45b4-7a8f-4c8c-9e48-819fab0c19c4) to Async(default) with arguments: "Z2lkOi8vY2lhby9Qb3N0LzM",{:action=>:replace,:target=>#<GlobalID:0x00007fa9401ea938 @uri=#<URI::GID gid://Ciao/Comment/16>>,:locals=>{:comment=>#<GlobalID:0x00007fa9401ea0f0 @uri=#<URI::GID gid://Ciao/Comment/16>>},:partial=>"comments/comment"}
No template found for CommentsController#create,rendering head :no_content
Completed 204 No Content in 37ms (ActiveRecord: 8.5ms | Allocations: 8931)

Turbo 似乎可以在数据库中创建评论并发回评论 POST 请求,我可以在浏览器网络选项卡中看到该请求。我想知道为什么有 :no_content 以及为什么直到页面刷新才显示评论

Place.rb

class Post < ApplicationRecord
  belongs_to :place
  has_many :comments
  broadcasts
end

Comment.rb

class Comment < ApplicationRecord
  belongs_to :post
  broadcasts_to :post
end

comment_controller.rb

def new
    @comment = @post.comments.new
  end

  def create
    @comment = current_user.comments.create!(comment_params)
    respond_to do |format|
      if @comment.save
         format.turbo_stream do
             render turbo_stream: turbo_stream.append(@comment,partial: 'comments/comment',locals: { comment: @comment })
        end
        format.html { redirect_to @comment.post.place }
      end
    end
  end

在帖子中,我将评论呈现为部分:

 <div class="post__comments--inner">
   <%= render '/comments/show',post: post %>
 </div>

然后comments/_show.html.erb

  <%= turbo_stream_from post %>
  <%= render post.comments %>
  <% puts post.comments %>
  <div class="comments__post">
    <%= turbo_frame_tag "new_comment",src:  new_post_comment_path(post),target: "_top" %>
  </div>

_comment.html.erb

<div class="comment" id="<%= dom_id comment %>">
  <%= comment.content %>
</div>

new.html.erb

<%= turbo_frame_tag "new_comment",target: "_top" do %>
  <%= form_with model: [@comment.post,@comment],class: "comment-row__form",data: { controller: "reset_form",action: "turbo:submit-end->reset_form#reset" }  do |form| %>
      <%= form.text_area :content,class: "comment-form--input form-control",data: {target: "comments.body"} %>
      <%= form.hidden_field :post_id,value: @comment.post.id %>
      <%= form.submit "comment",class: "btn btn-primary mt-2 mt-sm-0 ml-sm-3" %>
 
  <% end %>
<% end %>

我想我可能已经发现了问题,但我不确定为什么会发生这种情况,日志中的最后一个想法是:

Turbo::StreamsChannel transmitting "<turbo-stream action=\"replace\" target=\"comment_36\"><template>  <div class=\"comment\" id=\"comment_36\">\n    <div class=\"comment__user\">\n

我认为应该是 action=\"append\" 而不是替换,特别是因为 comment_36 目前在页面上还不存在。

解决方法

尝试按照文档中的示例进行操作:

 respond_to do |format|
  format.turbo_stream do
    render turbo_stream: turbo_stream.append(:comments,partial: "comments/comment",locals: { comment: @comment })
  end
 end

这应该将其附加到您的 html 中的 div 中,并带有注释 id。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?