讓我開始吧,我是新手,在rails中編程並試圖通過構建項目來學習。我正在創建一個項目,它具有類似於Twitter的關注和跟隨者功能...我已經實施了刪除帖子的選項。但是,似乎我可以刪除其他人張貼以及我正在關注等。我如何實現刪除我自己的帖子,並讓其他用戶有能力編輯修改和刪除自己的帖子。如何在Rails中實現刪除你的帖子而不是其他用戶帖子
post.rb
class Post < ActiveRecord::Base
belongs_to :user
validates :user_id, presence: true
validates :content, presence: true, length: { maximum: 140 } #
default_scope -> { order(created_at: :desc) } # newest tweets/posts first
end
柱控制器
def destroy
@status_update = Post.find(params[:id])
if @status_update.present?
@status_update.destroy
end
redirect_to root_url
末
家
<%= link_to('Delete', post_path(@p), :method => :delete,data: { confirm: "Are you sure?" }) %>
我也在尋找這樣的事情:
def owned_post
unless current_user == @post.user
flash[:alert] = "That post doesn't belong to you!"
redirect_to root_path
end
end
我想說謝謝所有你! –