上記は削除されたコメントの結果です。コメントを削除すると、コメントの親投稿も削除されることに注意してください。redirect_to
Started DELETE "/posts/19/comments/30" for 127.0.0.1 at 2012-12-03 01:10:43 -0800
Processing by CommentsController#destroy as JS
Parameters: {"post_id"=>"19", "id"=>"30"}
Comment Load (0.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = ? LIMIT 1 [["id", "30"]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", "19"]]
CACHE (0.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = ? LIMIT 1 [["id", "30"]]
(0.0ms) begin transaction
SQL (0.2ms) DELETE FROM "comments" WHERE "comments"."id" = ? [["id", 30]]
(7.7ms) commit transaction
Redirected to http://localhost:3000/posts/19
Completed 302 Found in 13ms (ActiveRecord: 8.4ms)
Started DELETE "/posts/19" for 127.0.0.1 at 2012-12-03 01:10:43 -0800
Processing by PostsController#destroy as JS
Parameters: {"id"=>"19"}
Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", "19"]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
CACHE (0.0ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", "19"]]
(0.0ms) begin transaction
Comment Load (0.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = 19
SQL (0.2ms) DELETE FROM "posts" WHERE "posts"."id" = ? [["id", 19]]
(1.1ms) commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 6ms (ActiveRecord: 1.7ms)
Started DELETE "/" for 127.0.0.1 at 2012-12-03 01:10:43 -0800
Processing by PagesController#home as JS
Rendered pages/home.html.haml within layouts/application (0.1ms)
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Completed 200 OK in 40ms (Views: 39.3ms | ActiveRecord: 0.2ms)
ルート.rb
resources :posts do
member do
put "soft_destroy"
end
resources :comments do
member do
get "reply"
post "create_reply"
put "soft_destroy"
end
end
end
コメントコントローラー
def destroy
@post = Post.find(params[:post_id])
@comment = Comment.find(params[:id])
@comment.destroy
redirect_to @post
end
ビューファイルのリンクを削除する
= link_to "delete", [@post, comment], method: :DELETE, remote: true
ポストモデル
has_many :comments, dependent: :destroy
accepts_nested_attributes_for :comments
コメントモデル
belongs_to :post
DELETE html動詞がpostsコントローラーでも伝播する理由はありますか?show
単にアクションを呼び出すのではなく?