_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个