【问题标题】:Hotwire turbo streams returning head :no_contentHotwire turbo 流返回 head :no_content
【发布时间】:2021-07-24 13:48:31
【问题描述】:

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

一切正常,数据被持久化到数据库中,但涡轮流似乎没有正确返回。当我点击“评论”按钮时,没有任何变化,我收到一条 :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 似乎可以在数据库中创建评论,然后发回我可以在浏览器网络选项卡中看到的 cmets POST 请求。我想知道为什么有 :no_content 以及为什么 cmets 在页面刷新之前不显示。

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 目前还没有出现在页面上。

【问题讨论】:

  • 你没有在块中渲染任何东西,例如 format.turbo_stream { render turbo_stream: turbo_stream.append(@comment) }
  • 我只是从他们拥有的热线演示中复制:respond_to do |format|format.turbo_streamformat.html { redirect_to @room }end我添加了一个渲染块,它成功返回但没有附加任何内容。我已经更新了我的问题以反映这些变化。
  • 它确实需要附加,因为它是一个新评论
  • 这就是为什么我很困惑为什么涡轮流频道说动作是替换。
  • 我可能在上面给出了一个糟糕的 sn-p,也许试试我的答案

标签: ruby-on-rails hotwire-rails turbo


【解决方案1】:

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

 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 为 cmets。

【讨论】:

  • 感谢您的原始评论是朝着正确方向迈出的重要一步,我认为仍然没有渲染的原因与 target="comments" 的指定有关,因为页面上的每个帖子都有一个 cmets 字段我认为 Turbo 不知道在哪里追加。
  • 确实是在寻找一个带有 dom id cmets 的元素来追加。
猜你喜欢
  • 2021-06-17
  • 1970-01-01
  • 1970-01-01
  • 2021-09-13
  • 2022-01-16
  • 2021-12-11
  • 1970-01-01
  • 2022-12-29
  • 2018-04-16
相关资源
最近更新 更多