Rails Routes

Wendy Raven McNair
3 min readMay 24, 2021
Photo by Chait Goli from Pexels

In my last post, RESTful Routes, I spoke about how the server communicates with the client browser. In this post, I’ll extend that communication from being routed through the Rails routes.rb file to the appropriate action located in the correct controller.rb file.

Ruby on Rails or Rails is a back end web application framework. That means it’s a server side framework. Rails receives requests from the client front end, processes the requests then returns the result. Note that rails can return results using the files in its views directory or it can return results using json to be delivered to a front end library like React.

Rails receives these requests via routes located in the config/routes.rb file. Using the songs example from my last post, the URL and the HTTP method would be extracted from an incoming front end communication similar to the following:

fetch(`http://localhost:3000/songs`, {

method: ‘GET’

})

The router is responsible for sending the request to the correct controller.rb file. So songs in the URL indicates the request should be routed to the songs controller file. Inside of that file are actions (methods) and the router is also responsible for getting the request to the appropriate action. The HTTP method GET indicates the index action should handle the request.

To achieve this, the incoming communication would be matched to the following Rails route in the routes.rb file:

get ‘/songs’, to: ‘songs#index’

Inside of each action are methods or code that will perform the requested action. In this case the index action is as follows:

def index

@songs = Song.all
render json: @songs

end

Inside of the index action, the @songs instance is being assigned all of the songs contained in the database with Song.all. Then render json: @songs returns converts the collection of songs to json and returns it to the front end. Note I’m only detailing the internal content of the index action in this post for clarity. However, I will detail all seven actions in a future post.

Step by step detail of the process:

— The URL identifies which controller.rb file to send the request.

— The URL http://localhost:3000/songs would be routed to the songs_controller.rb file.

— Inside of that file, the GET method would select the index action

— The index action would get all of the songs in the database and return them for display in the client browser.

This process is conducted for all seven RESTful routes which make up the CRUD actions; a request comes in from the client front end, the request is routed through the routes.rb file where the route is matched to a Rails route, which sends the request to the proper controller and action.

NOTE

— The matches are shown below with the incoming request indicated on the left of the => and the Rails matching route indicated on the right of the =>

— The Rails matching route indicates the controller file on the left of the # and the action on the right of the # (controller#action)

GET: /songs => songs#index

see a list of all the songs

GET: /songs/new => songs#new

see a form to create a new song

GET: /songs/:id => songs#show

see a single specific song (from the list of songs)

GET: /songs/:id/edit => songs#edit

see a form to edit the data of a specific song

POST: /songs => songs#create

add a new song to the list of songs

DELETE /songs/:id => songs#destroy

delete a specific song (from the list of songs)

PUT /songs/:id => songs#update

change the data of a specific song

-or-

PATCH /songs/:id => songs#update

change the data of a specific song

For more details on Rails routing consult https://guides.rubyonrails.org/routing.html

My previous post, RESTful Routes, detailed how the front end client browser communicated with the back end server using RESTful Routes. And this post detailed how that incoming communication is matched in the routes.rb file and routed to the correct controller.rb file and the appropriate action located inside of that file. My next post will detail what’s going on inside of each of the actions.

TRILOGY: Front End and Back End Communication

RESTful Routes
Rails Routes
Quiet on the Set… ACTION!

--

--