Rails 4 - 销毁删除不同的ID到params中的内容

时间:2014-05-22 13:04:19

标签: ruby-on-rails

当我尝试删除通知/帖子时,我得到以下内容:

Started DELETE "/notices/8" for 127.0.0.1 at 2014-05-22 17:34:46 +0800
Processing by NoticesController#destroy as HTML
  Parameters: {"authenticity_token"=>"...", "id"=>"8"}
  Notice Load (0.4ms)  SELECT "notices".* FROM "notices" WHERE (8) LIMIT 1
   (0.1ms)  begin transaction
  SQL (0.4ms)  DELETE FROM "notices" WHERE "notices"."id" = ?  [["id", 7]]
   (4.9ms)  commit transaction
Redirected to http://localhost:3000/notices?edit_mode=true
Completed 302 Found in 10ms (ActiveRecord: 5.9ms)

所以我刚刚将ID传递给了8,但它删除了ID 7,我不知道为什么。

控制器看起来像:

class NoticesController < ApplicationController
 def destroy
  @notice = Notice.find_by(params[:id])
  @notice.destroy
  redirect_to :back
 end
end

删除链接的视图模板:

<%= link_to "Delete", notice_path(@notice.id), class: "button", method: :delete %>

我不知道为什么它不起作用。 我重新启动了服务器,认为它可能是路由问题,但错误仍然存​​在。有什么想法吗?

另外,如果我遗漏了任何信息,请告诉我。这是我的第一个问题。

1 个答案:

答案 0 :(得分:0)

将控制器更改为: -

class NoticesController < ApplicationController
 def destroy
  @notice = Notice.find(params[:id])
  @notice.destroy
  redirect_to :action => "index"
 end
end

在视图中: -

<%= link_to "Delete", @notice, class: "button", method: :delete %>