只允许创建 post 的用户使用 Devise 编辑、更新或删除 post

Only allow the user that created the post to edit, update or delete the post with Devise

目前,我已经在 post 上向用户隐藏了他们尚未创建的按钮。我怎样才能阻止他们更新、编辑或删除 posts_controller 中的 post?我尝试在编辑、更新和删除方法周围使用 if @post.user == current_user,但那只是 returns 堆栈跟踪:

Missing template posts/update, application/update with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "/Users/user1/workspace/webapp/app/views" * "/Users/user1/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/devise-3.5.2/app/views"

如何获取正在尝试编辑 post 的用户,如果他不是所有者,如何阻止他编辑、更新或删除?

您必须有一个用于未经授权访问的模板,或者您可以重定向到 url if @post.user == current_user。喜欢下面

 if @post.user == current_user
    flash.now[:error] = 'unauthorized access!'
    redirect_to posts_path
 end