我得到我的應用程序的工作,以便用戶只能刪除自己的意見,但現在即時嘗試添加一個鏈接到網站,但我只希望評論的創建者能夠看到鏈接。顯示刪除鏈接創建者的評論
我試着做
<% 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
假設你實際上是存儲用戶的':id'在':你'comments'表user_id'列,你最後一次嘗試應該是工作得很好**對於'current_user'實際創作的任何評論**。除此之外,您確實需要展示更多的代碼才能幫助我們。 – deefour
我在我的評論表中有user_id ....最後一段代碼是我的評論控制器中的最新版本,以便只有創建評論的人才能刪除它...我認爲它應該可以工作,但是當我把它在我看來所有的刪除鏈接消失 – user1502223
可能是因爲你的'current_user'沒有編寫任何評論。再次,除非您向我們展示您的視圖和控制器,否則我們無法幫助您。當我們只是猜測你的代碼可能看起來像什麼時,我們無法真正提供幫助。 – deefour