redirect_to方法在整个嵌套路由中传播 DELETE http 操作



以上是删除注释的结果。请注意,当您删除评论时,评论的父帖子也会通过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

删除 html 动词也在帖子控制器上传播有什么原因吗?而不仅仅是呼吁show行动?

问题是由查看文件上的删除链接引起的

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

出于某种原因,带有方法 DELETE 的 ajax 请求似乎传播到第一个 DELETE 请求之外。

我删除了remote: true,它现在向帖子发出 GET 请求而不是 DELETE 请求。

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

我仍然不明白为什么会发生这种情况。

最新更新