I am following along with the Ruby on Rails tutorial guide for getting started and am in section 5.12 Using partials to clean up duplication in views. I understand why partials are used per the D.R.Y. convention, however, I am curious about a difference in the new.html.erb and edit.html.erb. Specifically, prior to using the partial _form.html.erb file, the edit.html.erb file explicitly called method: :patch
<%= form_for :article, url: article_path(@article), method: :patch do |f| %>
<% if @article.errors.any? %>
<div id="error_explanation">
...
... and now the _form.html.erb file covers both new & edit without explicitly calling PATCH:
<%= form_for @article do |f| %>
<% if @article.errors.any? %>
<div id="error_explanation">
...
Is PATCH still being invoked "behind the scenes" for editing?
As mentioned in the tutorial you shared:
Also, from the reference given there:
For an existing
resource
orrecord
for@post
, it is equivalent to:Whereas, for a new record of the resource, i.e.
Post.new
, it is equivalent toThus, in this case, magic lies in the rails with 2 things, helping rails understand what to do with this form:
resources :article
resource
object passed toform_for
.Yes.
form_for
checks whether the model is persisted to know whether it needs to perform aPOST
(create) orPATCH
(update).