Rails generates a form partial that can be used on both the page rendered by a get action and a page rendered by a new action. If it is the former the form's method is set to PUT, if the latter the form's action is set to POST.
How does rails decide which method to use?
if the object passed to the form is
persisted?
, the form builder knows that you are updating an object and will therefore render aPUT
action. If it is not persisted, then it knows you are creating a new object and it will usePOST
.If
@user
is a new record,POST
is used and the button label becomesCreate User
, otherwisePUT
is used and the label becomesUpdate User
. There's not much more to it.Forms editing existing resources use
PUT
, forms creating a new resource usePOST
. As per REST standards described here.From the rails
form_for
helper code:and
persisted?
for ActiveRecord is declared as: