“删除”动作发生两次?

发布于 2024-10-19 07:34:53 字数 849 浏览 1 评论 0原文

我的文件控制器的删除操作遇到了奇怪的情况。

Started GET "/files/35/delete" for 127.0.0.1 at 2011-02-27 01:13:51 -0500
Processing by FilesController#delete as HTML
Parameters: {"id"=>"35"}
SQL (0.3ms)  DELETE FROM `files` WHERE (`files`.`id` = 35)
SQL (0.7ms)  COMMIT
Redirected to http://localhost:3000/files
Completed 302 Found in 713ms

Started GET "/files/35/delete" for 127.0.0.1 at 2011-02-27 01:13:52 -0500
Processing by FilesController#delete as HTML
Parameters: {"id"=>"35"}
...leads to error

控制器操作:

@file = @company.files.where("id = ?", params[:id]).first
@file.destroy
flash[:notice] = "Your file was deleted successfully."
redirect_to files_url

路线:

resources :files do
  member do
    get 'delete_ask'
    get 'delete'
  end
end

你知道为什么会发生这种情况吗?

I have a strange situation with the delete action of my files controller.

Started GET "/files/35/delete" for 127.0.0.1 at 2011-02-27 01:13:51 -0500
Processing by FilesController#delete as HTML
Parameters: {"id"=>"35"}
SQL (0.3ms)  DELETE FROM `files` WHERE (`files`.`id` = 35)
SQL (0.7ms)  COMMIT
Redirected to http://localhost:3000/files
Completed 302 Found in 713ms

Started GET "/files/35/delete" for 127.0.0.1 at 2011-02-27 01:13:52 -0500
Processing by FilesController#delete as HTML
Parameters: {"id"=>"35"}
...leads to error

The controller action:

@file = @company.files.where("id = ?", params[:id]).first
@file.destroy
flash[:notice] = "Your file was deleted successfully."
redirect_to files_url

Routes:

resources :files do
  member do
    get 'delete_ask'
    get 'delete'
  end
end

Do you know why this is happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时常饿 2024-10-26 07:34:53

我怀疑这是由于重复提交请求而发生的。尝试通过 javascript 在客户端或服务器端防止双重提交(这将需要比客户端更多的努力,但更强大)。

I suspect that this happens due to double submit of the request. Try to prevent the double submits either at the client side through javascript or on the server side (which will require some more effort than the client side but more robust).

遥远的她 2024-10-26 07:34:53
@file = @company.files.where("id = ?", params[:id]).first
if @file
    @file.destroy
    flash[:notice] = "Your file was deleted successfully."
else
    #file was deleted
    flash[:notice] = "Patience: you only need to press delete once" 
end
redirect_to files_url

您可以拯救错误:

@file = @company.files.where("id = ?", params[:id]).first
    @file.destroy
    flash[:notice] = "Your file was deleted successfully.
#rescue from error
rescue 
    flash[:notice] = "Patience: you only need to press delete once" 
    redirect_to files_url
@file = @company.files.where("id = ?", params[:id]).first
if @file
    @file.destroy
    flash[:notice] = "Your file was deleted successfully."
else
    #file was deleted
    flash[:notice] = "Patience: you only need to press delete once" 
end
redirect_to files_url

You can rescue from errors:

@file = @company.files.where("id = ?", params[:id]).first
    @file.destroy
    flash[:notice] = "Your file was deleted successfully.
#rescue from error
rescue 
    flash[:notice] = "Patience: you only need to press delete once" 
    redirect_to files_url
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文