Rails 101 (Rails 5版) 刪除發表過的文章

Rails 101 (Rails 5版) 刪除已發表的文章

修改 app/controllers/posts_controller.rb

1
2
3
4
5
6
7
8
-  before_action :authenticate_user!, only: [:new, :create]
+ before_action :authenticate_user!, only: [:new, :create, :destroy]

+ def destroy
+ @post = Post.find(params[:id])
+ @post.destroy
+ redirect_to account_posts_path, alert: "Post deleted"
+ end

成果

Reference

Rails 101 (Rails 5版) 額外作業