首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Hotwire turbo streams返回head :no_content

Hotwire turbo streams返回head :no_content
EN

Stack Overflow用户
提问于 2021-05-01 21:58:09
回答 1查看 770关注 0票数 0

我有一个系统与帖子和评论,每个Post has_many Comments。我正在尝试设置一个涡轮流,以便当您发布评论时,它立即显示。

所有的数据都被持久化到数据库中,但是似乎turbo流没有正确地返回。当我点击"comment“按钮时,一切都没有改变,我收到了一条CommentsController#create:no_content消息

代码语言:javascript
复制
  ↳ 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

代码语言:javascript
复制
class Post < ApplicationRecord
  belongs_to :place
  has_many :comments
  broadcasts
end

Comment.rb

代码语言:javascript
复制
class Comment < ApplicationRecord
  belongs_to :post
  broadcasts_to :post
end

comment_controller.rb

代码语言:javascript
复制
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

在这篇文章中,我将评论呈现为部分:

代码语言:javascript
复制
 <div class="post__comments--inner">
   <%= render '/comments/show', post: post %>
 </div>

然后是comments/_show.html.erb

代码语言:javascript
复制
  <%= 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

代码语言:javascript
复制
<div class="comment" id="<%= dom_id comment %>">
  <%= comment.content %>
</div>

new.html.erb

代码语言:javascript
复制
<%= 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 %>

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

代码语言:javascript
复制
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目前还不存在于页面上。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-02 04:53:57

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

代码语言:javascript
复制
 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。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67347079

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档