・ Rails 6.0.3.2 ・ Mysql Ver 14.14 Distrib 5.6.47 ・ Osx10.15
When I was creating a web application for the first time with Rails, I was wondering if routing
could work even though the HTTP method
was not specified when using form_with
or link_to
.
It takes a URI Patternand a
HTTP method and is responsible for specifying which action to take. So you need to specify ʻURI Pattern
and HTTP method
on the browser side.
A description that a certain action moves when the detail button is pressed (this is Prefix)
<%= link_to 'Details', tweet_path(tweet.id), method: :get %>
Toka (this is URI Pattern)
<%= link_to 'Delete', "/tweets/#{tweet.id}", method: :delete %>
HTTP method specification and ʻURI Patternare specified in
method:`
Specifically, the following description
<%= link_to 'Details', tweet_path(tweet.id) %>
Compared to the above example, it works fine without method:: get
.
that? I didn't specify the HTTP method? ?? ??
For me as a beginner, why does it work when there is no designation? It was the moment when I didn't understand the rules of routing. .. ..
If you don't specify the HTTP method
, the browser will send a request to the server with GET
by default!
It's just a browser story, regardless of the routing of Rails
.
(* Corrected by pointing out in the comments)
So even if you don't specify it, the browser will send a ʻURI Patternand a
HTTP method`, and an action will work! !!
It was a feeling that I, a beginner, couldn't quite understand that it would work even though it was not specified.
Rails routing ・ Https://railsguides.jp/routing.html
Recommended Posts