Ruby On Rails: JSON Requests
Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for your
destroy actions, a resourceful route declares them in a single line of code.
Browsers request pages from Rails by making a request for a URL using a specific HTTP method, such as
DELETE. Each method is a request to perform an operation on the resource. A resource route maps a number of related requests to actions in a single controller.
- GET - how a client machine tells a server that it wants information about the item identified by the URI. Because GET requests are all about asking for information, they are not permitted to have request bodies. You still have the URI query string available to you if you need to send data from the client to the server on a GET request.
- POST - how a client tells a server to add an entity as a child of the object identified by the URI. The entity that the client expects the server to add is transmitted in the request body.
- PATCH - how a client tells a server it wants to modify an object identified by the URI the request is sent to.
- DELETE - as you might guess, how a client tells a server to remove an object identified by the URI the request is sent to.
In this tutorial, I'll be explain you about JSON requests on Rails. JSON is a lightweight data-interchange format. It is easy for humans to read and write. So essentially, it's a simple way to store and exchange data, regardless of which language we use.
The above snippet returns with the hashed values as shown.
Using Post method
Execute the above snippet , Pass the values in params. It will create a new id with respect to values which you been passed.
Using Put method, Update the Model attributes based on the id.
Use Delete same as above by replacing Put or Post to Delete.