[RUBY] If the HTTP method is not specified, the browser side requests with GET

environment

・ Rails 6.0.3.2 ・ Mysql Ver 14.14 Distrib 5.6.47 ・ Osx10.15

Introduction

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.

What is routing?

It takes a URI Patternand aHTTP method and is responsible for specifying which action to take. So you need to specify ʻURI Pattern and HTTP method on the browser side.

For example

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 inmethod:`

[Main subject] When HTTP method is not specified

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. .. ..

Conclusion

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 aHTTP 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.

reference

Rails routing ・ Https://railsguides.jp/routing.html

Recommended Posts

If the HTTP method is not specified, the browser side requests with GET
Setting method that the size does not change even if CSS is changed
Why the get method is needed in the Calendar class
[Rails] What to do if you can't get an error message with the errors method
Now is the time to get started with the Stream API
What to do if the app is not created with the latest Rails version installed when rails new
What is the pluck method?
What is the initialize method?
[Ubuntu 20.04] What to do if the external monitor is not recognized
If the DB container does not start with Initializing database files
Use Modifier # isStatic to determine if the [Reflection] method is static.
Phenomenon that HTTP becomes GET even if method:: delete in link_to method
Resultset's next () is not a "method to determine if there is a ResultSet next".
A monad is a design pattern for "expressing the existence of side effects in a method with a return type".