Dispatching Requests Through REST
Learn to use REST for dispatching requests.
At its most basic, a web application accepts an incoming request from a browser, processes it, and sends a response.
The first question that springs to mind is, how does the application know what to do with the incoming request? A shopping cart application will receive requests to display a catalog, add items to a cart, create an order, and so on. How does it route these requests to the appropriate code?
It turns out that Rails provides two ways to define how to route a request:a comprehensive way to use when we need to and a convenient way that we’ll generally use whenever we can:
- The comprehensive way lets us define a direct mapping of URLs to actions based on pattern matching, requirements, and conditions.
- The convenient way lets us define routes based on resources, such as the models we define. Because the convenient way is built on the comprehensive way, we can freely mix and match the two approaches.
In both cases, Rails encodes information in the request URL and uses a subsystem called Action Dispatch to determine what should be done with that request. The actual process is very flexible, but at the end of it, Rails has determined the name of the controller that handles this ...