gpt4 book ai didi

ruby-on-rails - Destroy 操作中的 Redirect_to 始终会获取 DELETE 动词,无论 :method I declare

转载 作者:行者123 更新时间:2023-12-03 01:53:35 27 4
gpt4 key购买 nike

我在名为 tareas_controller 的 Controller 中有以下方法

def destroy
@tarea = Tarea.find(params[:id])
@tarea.destroy

respond_to do |format|
format.html { redirect_to tareas_url }
format.json { head :ok }
format.js { redirect_to :controller => "clientes", :action =>"show", :id => @tarea.cliente, :format => :js, :method=>:get}
end

end

记录被删除了,之后我在服务器上得到以下代码:

Redirected to http://127.0.0.1:3000/clientes/12.js?method=get Completed 302 Found in 174ms

Started DELETE "/clientes/12.js?method=get" for 127.0.0.1 at 2012-05-06 19:20:07 +0200 Processing by ClientesController#destroy as JS Parameters: {"method"=>"get", "id"=>"12"} Cliente Load (0.0ms) SELECT "clientes".* FROM "clientes" WHERE "clientes"."id" = ? LIMIT 1 [["id", "12"]] SQL (2.0ms) DELETE FROM "clientes" WHERE "clientes"."id" = ? [["id", 12]] Completed 406 Not Acceptable in 131ms

它似乎将带有 DELETE 动词的请求发送到新 Controller ,但我找不到方法将其更改为对新 Controller 的 GET 请求。

有人可以给我建议如何解决这个问题吗?

最佳答案

您应该使用状态 303 进行重定向。

If you are using XHR requests other than GET or POST and redirecting after the request then some browsers will follow the redirect using the original request method. This may lead to undesirable behavior such as a double DELETE. To work around this you can return a 303 See Other status code which will be followed using a GET request.

  redirect_to posts_url, status: :see_other
redirect_to action: 'index', status: 303

来源:http://api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-redirect_to

关于ruby-on-rails - Destroy 操作中的 Redirect_to 始终会获取 DELETE 动词,无论 :method I declare,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10472600/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com