2012-12-17 30 views
0

我得到我的應用程序的工作,以便用戶只能刪除自己的意見,但現在即時嘗試添加一個鏈接到網站,但我只希望評論的創建者能夠看到鏈接。顯示刪除鏈接創建者的評論

我試着做

<% 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

+2

假設你實際上是存儲用戶的':id'在':你'comments'表user_id'列,你最後一次嘗試應該是工作得很好**對於'current_user'實際創作的任何評論**。除此之外,您確實需要展示更多的代碼才能幫助我們。 – deefour

+0

我在我的評論表中有user_id ....最後一段代碼是我的評論控制器中的最新版本,以便只有創建評論的人才能刪除它...我認爲它應該可以工作,但是當我把它在我看來所有的刪除鏈接消失 – user1502223

+1

可能是因爲你的'current_user'沒有編寫任何評論。再次,除非您向我們展示您的視圖和控制器,否則我們無法幫助您。當我們只是猜測你的代碼可能看起來像什麼時,我們無法真正提供幫助。 – deefour

回答

1
<% if comment.user_id == current_user.id %> 

,並應工作)

+0

謝謝你,awsome – user1502223

+0

但是,如果current_user是零呢?未登錄?追加你的代碼:<%if current_user &&(comment.user_id == current_user.id)%>或<%if current_user &&(comment.user == current_user)%> –

+0

你的權利謝謝你修復問題 – user1502223

2

@comment更改爲comment在您的視圖中。

<% if comment.user_id == current_user.id %> 

@comment在您destroy行動定義的,但是當你安裝在您的視圖

<% @comments.each do |comment| %> 

@comment這樣的塊有沒有價值,只有comment一樣。

+0

多數民衆贊成在此非常感謝 – user1502223

+0

下次提供您的代碼將節省每個人的時間,讓你的答案更快。 – deefour

+0

會做謝謝! – user1502223