2

上記は削除されたコメントの結果です。コメントを削除すると、コメントの親投稿も削除されることに注意してください。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単にアクションを呼び出すのではなく?

4

1 に答える 1

1

この問題は、ビュー ファイルのリンクの削除が原因でした

= link_to "delete", [@post, comment], method: :DELETE, remote: true

何らかの理由で、メソッド DELETE を使用した ajax リクエストは、最初の DELETE リクエストを超えて伝播するようです。

削除remote: trueしたところ、投稿に対して DELETE リクエストではなく GET リクエストが行われるようになりました。

= link_to "delete", [@post, comment], method: :DELETE

なぜこれが起こっているのか、私はまだ理解していません。

于 2012-12-03T17:57:25.470 に答える