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了,它现在向帖子发出 GET 请求而不是 DELETE 请求。

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

我仍然不明白为什么会这样。

于 2012-12-03T17:57:25.470 回答