_posts.html.erb

    <div class="container col-sm-4 mx-auto mt-4">
        <% posts.each do |post| %>
        <%= turbo_frame_tag dom_id(post) do %>
            <div class="card text-center bg-info" style="width: 18rem;">
                <%= image_tag file_path(post.medias.first), class: "rounded mx-auto d-block" %>
                <h6 class="card-subtitle mb-2 text-body-secondary mt-2">by <%= post.user.first_name %> <%= post.user.last_name%> </h6>
                <div class="card-body">
                    <p class="card-text"><%= post.description %></p>
                    <%= link_to "View Post", post_path(post), class: "btn btn-dark" %>
                </div>
                <div class="card-footer text-body-secondary">
                    <%= time_ago_in_words(post.created_at) %> ago
                </div>
                <div class="row justify-content-between mb-2">
                    <% if current_user_post_like?(post) %>
                        <div class="col-sm-3"> 
                            <%= post.likes.count %>
                            <%= link_to "Unlike", likes_path({id: post}), data: {turbo_stream: true, turbo_method: :delete} %>
                        </div>
                    <% else %>
                        <div class="col-sm-3"> 
                            <%= post.likes.count %>
                            <%= link_to "Liked", likes_path({id: post}), data: {turbo_stream: true, turbo_method: :post} %>
                        </div>
                    <% end %>
                    <div class="col-sm-3"> 1 </div>
                    <div class="col-sm-3"> 1 </div>
                </div>
            </div>
            <% end %>

        <% end %> 
    </div>

Like_Controler.rb

class LikesController < ApplicationController
    before_action :fetch_post

    def create
        @like = Like.find_or_initialize_by(likeable: fetch_post, user: current_user)
        if @like.save
            @posts = Post.all
            render turbo_stream: turbo_stream.replace(
                @post,
                partial: 'shared/posts',
                locals: { posts: @posts }
                )
        end
    end

    def destroy
        @like = current_user.likes.find_by(likeable: @post)
        if @like.destroy
            @posts = Post.all

            render turbo_stream: turbo_stream.replace(
                @post,
                partial: 'shared/posts',
                locals: { posts: @posts }
                )
        end
    end

    private

    def like_params
        params.require
    end

    def fetch_post
        @post = Post.find_by(id: params[:id])
    end
end

Page_Checkler.rb

class PagesController \< ApplicationController
    def index
        @posts = Post.all
    end
end

我得到错误,当渲染部分与turbo_stream,设计被打破,重复的帖子显示.我正在获取页面控制器中的所有帖子,并在那之后部分呈现_post,当我点击它时,就像它应该用turbo_stream进行更新一样 enter image description here

推荐答案

您将用包括容器在内的所有帖子替换一个目标帖子,这最终会嵌套并复制所有内容.

# app/views/shared/_posts.html.erb -->

<%= render @posts %>

我不确定您是否真的需要Turbo帧,这不是必需的:

# app/views/posts/_post.html.erb

# this is your target that you want to replace
#               vvvvvvvvvvvv
<%= tag.div id: dom_id(post) do %>
  # TODO: your post layout
<% end %>

然后在您的控制器中,当您替换帖子时,您必须只呈现该帖子:

render turbo_stream: turbo_stream.replace(
  @post,                  # this implicitly generates target id with `dom_id(@post)`
  partial: "posts/post",  # which is replaced with this partial
  locals: {post: @post}
)

# can be shortened to
render turbo_stream: turbo_stream.replace(@post)

另一个类似的例子:100

Ruby-on-rails相关问答推荐

Rails 7 Active Record::SessionStore -如何获取控制器中当前会话的数据库ID?

我try 使用Ruby on Rails7创建一个Carbon 足迹计算器.但我无法保存和用户S对象

具有 IN 子句的参数化查询在 ruby​​-on-rails 的 blazer 工具中无法按预期工作

如何让删除链接响应 Rails 7 中的 turbo_stream 和 html?

Rails:当 id 方法为非标准时构建关联

组织和/或存储我一直在 RSpec 中使用的模拟对象的最佳方式是什么?

如何不需要rails/all?

Rails 7.0.4 和 link_to 的数据确认不起作用

从 rails 5 迁移到 rails 6

我真正运行的是哪个 Ruby 版本?

Rails 模型.有效吗?刷新自定义错误并错误地返回 true

Rails Scope 返回 all 而不是 nil

bundle和gem安装的区别?

警告:引用了顶级常量

如何检测我的代码是否在 Rails 3 的控制台中运行?

在范围内传递参数

在 Rails 中命名布尔列

如何在rails中正确使用单选按钮?

Rails 3:乘客找不到Bundle 程序安装的 git gem

在 Ruby on Rails 中设置日志(log)记录级别