Blankman
Blankman

Reputation: 267290

How to make create and update use the same route

I have a route and form that looks like this:

<%= form_for @address, url: {action: "update_contact", controller: "checkouts"}, html: {class: ""} do |f| %>

My route looks like:

post "checkouts/:cart_token/update_contact" => "checkouts#update_contact", as: "checkouts_update_contact"

For updates the form is looking for a PATCH which I haven't defined, and so I get an error when the @address model already exists i.e. updates

How can I make my form always POST no matter what?

Upvotes: 0

Views: 203

Answers (1)

Eyeslandic
Eyeslandic

Reputation: 14900

Add method: :post

<%= form_for @address, 
    url: {action: "update_contact", controller: "checkouts"}, 
    html: {class: ""}, 
    method: :post do |f| %>

Without that Rails adds a hidden field which is used to fake a PATCH request when the form is used to update an object.

<input type="hidden" name="_method" value="patch" />

Upvotes: 3

Related Questions