Resources and Routes: Ruby on Rails Assessment review

Ifeoluwa Akinremi Wade
2 min readJul 28, 2021

What does the resources macro do for you when you use it to define routes?

First of all, what is a macro?

A macro is a method that when called, creates methods for you. — Learn.co

AND secondly, What are routes!?

Every time a person clicks on a link, types in a URL, or submits a form, they are making HTTP requests to a specific URL on your application. Routes are the part of an application that connect HTTP requests to a specific method in your application code built to handle responding to such a request (that part of code is called a Controller Action). — Learn.co

Ok! Now that we got that out of the way, I’m going to proceed. So back to the initial question:

  1. What does the resources macro do for you when you use it to define routes?

Resources declare all the common routes (index, show, new, edit, create, update, and destroy) in a single line of code.
ie: resources :posts ← this single line of code will define → routes (index, show, new, edit, create, update and destroy).

Resources will also create routing methods in the controller

def index
enddef show
enddef new
enddef create
enddef edit
enddef update
enddef destroy
end

and URL helpers

  • new_geocoder_path returns /geocoder/new
  • edit_geocoder_path returns /geocoder/edit
  • geocoder_path returns /geocoder

that will be utilized in the controllers and views.

Routing and Nested Resources

2. What happens when you nest those routes? What is the benefit of having reference to one record’s id in another record’s controller?

Nested resources give us a way to document that parent/child relationship in our routes and, ultimately, our URLs.

parent: authors
child: posts

resources :authors, only: [:show] do# nested resource for postsresources :posts, only: [:show, :index]end

I would say — in my opinion, the benefit of referencing a record’s ID in another controller is DRYer code, established responsibility, and separation of concern. For example, in my project, I had reviews nested under doctors. I was able to take care of all that pertained to that parent/child relationship in my reviews controller.

--

--