redirect_to方法在嵌套路由中传播DELETE http动作

时间:2012-12-03 09:23:49

标签: ruby-on-rails nested-attributes

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

的routes.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谓词在帖子控制器上传播是否有原因?而不是仅仅调用show动作?

1 个答案:

答案 0 :(得分:1)

问题是由视图文件上的删除链接引起的

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

出于某种原因,使用方法DELETE的ajax请求似乎传播超出第一个DELETE请求。

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

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

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