参考情報:
...
I made a post model along with its generated controller,
but anyone can Edit and Destroy the posts.
How can i allow only the author to edit and destroy the post.
...
上のページでは、”投稿者だけが投稿した記事をしたい" という質問とその回答が記載されています。
回答欄から抜粋します。
<% if @post.user == current_user %>
(the links to edit and destroy)
<% end %>
つまり、記事の投稿者 == ログインしているユーザーの場合にだけ 削除機能へのリンクを表示するようにしています。
lang
1def destroy
2 if current_user == @post.user
3 @post.destroy
4 else
5 redirect_to @post, error: "You don't have permission."
6 end
7 respond_to do |format|
8 format.html { redirect_to posts_url }
9 format.json { head :no_content }
10 end
11end
つまり、ログインしているユーザー == 記事の投稿者 の場合は削除を実行しますが、
そうでない場合は エラーメッセージを表示します。
質問されている方の Rails アプリに そのまま適用はできませんが、
考え方は利用できるとおもいます。
それは、
curent_user (ログインしているユーザー情報) と記事の投稿者 を比較して処理をする
ということです。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。