0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Rails】投稿者のみに削除ボタン表示(メモ)

Posted at

#環境

  • Rails: 6.1.3
  • ruby: 3.0.0
  • mac: OS

前提

  • 投稿機能、ログイン機能実装済み

#方法

ビューに記述

削除ボタン設置前に、投稿者本人か確認するため、if logged_in? && current_user.id == post.user_idを記述。
今ログインしているユーザーidと投稿idが一致した場合にのみ削除ボタンが表示されます。

app/views/posts/index/html.erb
 <% if logged_in? && current_user.id == post.user_id %>
    <%= link_to '削除', post_path(post.id),method: :delete, data: { confirm: '本当にいいですか?' } %>
 <% end %>

こちらの場合も同様の手順で投稿者のみに削除ボタンが表示されます。

app/views/comments/_comment.html.erb
 <% if logged_in? && current_user.id == comment.user_id %>           
   <%= link_to '削除', [comment.post, comment],method: :delete,data: { confirm: "本当に削除してもよろしいですか?" } %>
 <% end %>

以上になります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?