對於每篇文章,都有後期評論。允許用戶在導軌中刪除他們自己的評論3
這裏是爲Postcomment模型的樣子:
class Postcomment < ActiveRecord::Base
attr_accessible :comment_content
belongs_to :user
belongs_to :post
end
和Post模型
class Post < ActiveRecord::Base
attr_accessible :content, :image, :comment_content
belongs_to :user
has_many :postcomments, dependent: :destroy
end
我想允許用戶刪除自己的postcomments。這是我已經在視圖
_postcomment.html.erb
<% if post.postcomments.exists? %>
<% post.postcomments.each do |postcomment| %>
<%= link_to postcomment.user.name, postcomment.user %>
<span class="content2"><%= postcomment.postcomment_content %></span>
<% end %>
<% end %>
如何把這個代碼下面被改變,以允許用戶刪除評論?
<% if current_user?(postcomment.user) %>
<%= link_to "delete", postcomment.content, method: :delete,
confirm: "You sure?",
title: postcomment.content %>
<% end %>
current_user
方法在會話的輔助
def current_user=(user)
@current_user = user
end
def current_user
@current_user ||= User.find_by_remember_token(cookies[:remember_token])
end
def current_user?(user)
user == current_user
end
這裏的Postcomments表
create_table "postcomments", :force => true do |t|
t.text "content"
t.integer "user_id"
t.integer "post_id"
t.timestamp "created_at", :null => false
t.timestamp "updated_at", :null => false
t.text "comment_content"
end
有什麼問題你的代碼?它會給出錯誤嗎?它不會讓用戶刪除他們的評論嗎? – Catfish
它不會給出錯誤...但「刪除」文本不會顯示在評論旁邊 – user2159586
您是否在使用設計? – Catfish