我的文件控制器的删除操作有一个奇怪的情况。
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
你知道为什么会这样吗?
答案 0 :(得分:2)
我怀疑这是因为请求的双重提交而发生的。尽量避免在客户端通过javascript或服务器端提交双重提交(这需要比客户端更多的努力,但更强大)。
答案 1 :(得分:0)
@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
您可以rescue
出错:
@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