Request Response Cycle. The Refresher.
After being in JavaScript for 6 weeks I was basically phoning in my routes.
resource this, resource that, yada, yada, yada…
So let’s take a little refresher on how our friends the request response cycle, RESTFUL routing, and unpack resources.
Request Response Cycle:
Below we have a basic representation on the request response cycle.
Breaking it down from the users perspective.
- The user hops on their browser, types in a URL, and presses Enter.
- The Browser then makes a request for that URL.
- The request hits the Rails router (config/routes.rb) The router maps the URL to the correct controller and action.
- The action gets the request and passes it on to the view.
- The view renders the page as HTML
- The controller sends the HTML back to the browser. The pages loads for the user to see it.
Restful Routing
from Learn.co…
“A RESTful route is a route that provides mapping between HTTP verbs (get, post, put, delete, patch) to controller CRUD actions (create, read, update, delete). Instead of relying solely on the URL to indicate what site to visit, a RESTful route also depends on the HTTP verb and the URL.”
As you can see above the route name is what you will be defining in your controller.rb and will correspond with their respective HTTP Verbs.
Below is an example of a controller.rb file with CRUD actions.
Unpacking resources.
Using resources in your routes.rb file is the quickest way to declare all the common routes for a specific controller.rb without having to declare each one individually.
You can limit your resources by using :only [ whatever routes you want listed here ]. You can also define multiple resources on one line.
In the picture above, line #2 is limiting routes to only the index and show action. On line #3 you’ll get all seven common routes going to the controller and along with line #5 for the declared controllers.
Let’s break it down even further and write each route out for :albums.
Below you see we are starting with our HTTP verb, then the URL, then “to” the controller we are looking for, then # the specific action on that controller. Custom routes should have similar syntax.
Thanks for going back in time with me and visiting our old friend rails and as they say….. rails never goes away….
If you want to dig into resources even more check out the links listed below.
Resources: