前提・実現したいこと
ある投稿にあるユーザーがコメントをしたとします。
コメントを送信してから削除したいと思ったそのユーザーが自分のコメントだけを削除できるようにしたいです。
また投稿をした本人は全てのコメントを削除できるようにしたい。
発生している問題・エラーメッセージ
_comments.html.erb
この状態だと全てのコメントを削除出来てしまう。
<% @post.comments.each do |comment| %> <div class="card post-com"> <div class="card-body com-body"> <p> <strong><%= comment.user.name %>: </strong> <%= comment.content %> </p> <%= link_to '削除', [comment.post, comment], method: :delete, class: "btn btn-danger" ,data: { confirm: "削除してもいいですか?" } %> </div> </div> <% end %>
該当のソースコード
comments_controller.rb
class CommentsController < ApplicationController def create @post = Post.find(params[:post_id]) @comment = @post.comments.create(comment_params) @comment.user_id = current_user.id if @comment.save redirect_to post_path(@post) else redirect_to root_path end end def destroy @post = Post.find(params[:post_id]) @comment = @post.comments.find(params[:id]) if @comment.destroy redirect_to post_path(@post) end end private def comment_params params.require(:comment).permit(:content) end end
試したこと
_comments.html.erb
<% if user_signed_in? == current_user %>で囲むことで実現できると思ったのですが無理でした。
また、<% if @user == current_user %>で囲んで見てもダメでした。
<% @post.comments.each do |comment| %> : <% if user_signed_in? == current_user %> <%= link_to '削除', [comment.post, comment], method: :delete, class: "btn btn-danger" ,data: { confirm: "削除してもいいですか?" } %> <% end %> : <% end %>
手助け是非お願いします
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/12 00:56
2019/01/12 01:09