0

I'm using Rails 4.2.3. In my coffee script, I’m manually editing the URL and action of a form to be like so …

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

But I submit my form (by clicking on my submit button …)

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

The request (on both Mac Chrome and Firefox) gets submitted with method = “GET” instead of what I changed it to. What do I need to do so that I can submit via a PATCH method? (I need to do this because this is what is required by my update action).

univerio
  • 19,548
  • 3
  • 66
  • 68
Dave
  • 15,639
  • 133
  • 442
  • 830

1 Answers1

0

method on <form> elements can only be get or post, not put or patch. See <form> on MDN.

Generally, you should restrict yourself to GET and POST requests when working with browsers for this reason, even though it's supported when using XHR.

univerio
  • 19,548
  • 3
  • 66
  • 68
  • If what you are saying is true, then how does Rails submit forms? In my config/routes.rb file, I ahve defined "resources :my_objects", which implies updates to "/my_objects/:id", require a "PUT/PATCH" method, at least according to the Rails documentation I'm reading. – Dave May 28 '16 at 12:23
  • @Dave You can specify [`_method=patch`](http://guides.rubyonrails.org/form_helpers.html#how-do-forms-with-patch-put-or-delete-methods-work-questionmark) as a form param on a POST request to get around the restriction. – univerio May 28 '16 at 17:34