ユーザーが自分のコメントのみを削除できるようにアプリを動作させましたが、サイトへのリンクを追加しようとしていますが、コメントの作成者だけがリンクを表示できるようにしたいです。
やってみた
<% if current_user %>
しかし、それを行うと、ユーザーだけでなくすべてのコメントに表示されます。できます
<% if current_user.admin %>
これにより、管理者のみが削除リンクを表示できるように制限されます。私も試してみました
<% if @comment.user_id == current_user.id %>
それもうまくいきません。コメントの作成者がコメントのみを表示できるようにするにはどうすればよいですか?
これが私の見解です
_comments.html.erb
<div class="container">
<% @comments.each do |comment| %>
<div class="comment">
<div class="gravatar_border">
<%= gravatar_for comment.user, size: '49' %>
</div>
<div class="user_info_comments">
<b><%= comment.user.name %></b>
<%= comment.created_at.strftime("%b. %d %Y") %>
</div>
<div class="user_comments">
<%= simple_format comment.content %>
<%= pluralize comment.reputation_value_for(:votes).to_i, "vote" %>
<%= link_to "", vote_comment_path(comment, type: 'up'), method: "post", class: ' icon-thumbs-up' %>
<%= link_to "", vote_comment_path(comment, type: 'down'), method: "post", class: ' icon-thumbs-down' %>
<% if @comment.user_id == current_user.id %>
<%= link_to 'delete', comment, method: :delete, confirm: 'you sure?' %>
<% end %>
</div>
</div>
<% end %>
<br />
</div>
これが私のコントローラーです
コメント_コントローラー.rb
def destroy
@comment = Comment.find(params[:id])
if @comment.user_id == current_user.id
@comment.destroy
flash[:notice] = "comment deleted!"
else
flash[:error] = "not allowed"
end
redirect_to :back
end
サイトへのリンクはこちらhttp://www.batman-fansite.com