我让我的应用程序工作,以便用户只能删除自己的评论,但现在我试图添加指向该网站的链接,但我只希望评论的创建者能够看到链接。
我试过做
<% 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>
这是我的控制器
comments_controller.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
视图中将@comment
更改为comment
。
<% if comment.user_id == current_user.id %>
@comment
在destroy
操作中定义,但是当您在视图中设置这样的块时
<% @comments.each do |comment| %>
@comment
没有价值,只有comment
有价值。
<% if comment.user_id == current_user.id %>
并且应该工作)