As mentioned in the tutorial you shared:
The reason we can use this shorter, simpler form_for declaration to stand in for either of the other forms is that @article is a resource corresponding to a full set of RESTful routes, and Rails is able to infer which URI and method to use.
Also, from the reference given there:
For an existing resource
or record
for @post
, it is equivalent to:
<%= form_for @post, as: :post, url: post_path(@post), method: :patch, html: { class: "edit_post", id: "edit_post_45" } do |f| %>
...
<% end %>
Whereas, for a new record of the resource, i.e. Post.new
, it is equivalent to
<%= form_for @post, as: :post, url: posts_path, html: { class: "new_post", id: "new_post" } do |f| %>
...
<% end %>
Thus, in this case, magic lies in the rails with 2 things, helping rails understand what to do with this form:
- Your defined routes for your model,
resources :article
- The type(Existing record or a new record) of
resource
object passed toform_for
.
solved Does rails use method: :patch for editing with _form.html.erb?