Ruby on Rails Controller Explanation

Crystal Villanueva
4 min readFeb 22, 2021

This blog will discuss the purpose of each method in a controller class in Ruby on Rails. Class methods in Ruby on Rails are a part of the Model-View-Controller paradigm. This is a popular design pattern is used to define the data from the model to reflect real world situations (i.e., building a dog sitting app, the models could be ‘dog’ or ‘petsitter’), route it through the respective controller and then finally render the data to the view page. While the MVC design pattern is very important to understand in general, its good to know how and why which controller method works the way it does.

Index controller

def index         
@notes = Note.all
end

The index method represents all instances of the this particular data. For this example, all the songs in the database.

In the screenshot posted above, in the Rails console, you can see all the notes in the database. When you think of index, think of all the instances with that corresponding the controller, all the notes.

Show Controller

The opposite of showing all instances, would be to show one instance. In the method I have listed above, the show method, this lists one instance through out all the instances by finding the id.

In this screenshot I have listed above, I went through my database of notes, typed into my rails console, ‘ Note.find_by(id: 2)’ and the corresponding note with the id of 2 was returned. This is what the show method does and how it does it; it searches for a specific id.

The New and the Create Controller

The new method and the create method are very similar in the way that they work together. The new method has a corresponding view page, the ‘new’ view template. This means that on the front end of the application, the user will see a page with the route ‘/new’. A better way to think about how these methods work together is, ‘when a user wants to make a NEW instance, they must CREATE all the attributes of that new instance’. This means that when the song instance is created, the new method is received first, and then the create method creates and saves it to the database; once the new method is activates on the new page, behind the scenes of it the create method if activated, and a new instance of a song with all its attributes (song params in the example above) is created.

Edit and Update Controller

Like the new has a view page and the create method behind the scenes is activated, the edit and update methods work the same. The edit method has a corresponding edit view page the user will see and in Ruby on Rails which also hits the update method; once the edit method is activated, the update method will also update an existing instance in the database by finding its id and updating the information for that corresponding id; ActiveRecord in rails will first find the instance of the specific id it is looking for, then update its attributes depending on what the user wants to update about that model.

In the example listed above, the edit method will find the song instance by id, and then the update method will activate behind the scenes and about the song attributes.

Destroy Controller

The destroy controller works to find an instance in the database by searching for a specific id and deleting that instance from ActiveRecord entirely. All attributes in relation to that id of that song, note, whatever model it may be, will be deleted entirely.

Conclusion

While these methods were designed for Ruby on Rails, the ideology of Create, Read, Update and Delete (with new and edit) is universal across all programming languages, and is even more imperative to understand the basics of; CRUD is a way for programmers to create user interface for what their users can do. All popular websites allow for some CRUD related activity: Pinterest, Reddit, Gmail, etc. By understanding the basics of CRUD and how to implement it into your program, your user interactivity, will have a great start for what they can do!

--

--