如何使用我的 coffeescript 将表单方法更改为 PUT/PATCH?

How do I use my coffeescript to change the form method to PUT/PATCH?

我正在使用 Rails 4.2.3。在我的咖啡脚本中,我正在手动编辑表单的 URL 和操作,如下所示……

    # Alter the form submit behavior
    $('#new_my_object').attr("action", "/my_objects/" + id)
    $('#new_my_object').attr("method", "patch")

但是我提交了我的表格(通过点击我的提交按钮……)

<%= button_to "Save", { :action => "create" }, :method => :post, :class => 'button' %>

请求(在 Mac Chrome 和 Firefox 上)使用方法 = “GET” 提交,而不是我将其更改为的方法。我需要做什么才能通过 PATCH 方法提交? (我需要这样做,因为这是我的更新操作所要求的)。

method on <form> 元素只能是 getpost,不能是 putpatch。参见 <form> on MDN

通常,出于这个原因,您在使用浏览器时应该限制自己使用 GET 和 POST 请求,即使在使用 XHR 时也支持它。