What are rail inspectors? what is their role in an application? What challenges arise in their absence What is the inheritance hierarchy of controllers?

The Rails controller is the logical center of a rails application. It’s a type of class responsible for making sense of a request made in an application and producing appropriate output. It coordinates the interaction between the user, views, and the model. The controller also hosts several important ancillary services in processing user request data.

To have a good idea, let’s take an example, the metaphor gear lever and controller rails. as the image indicates, the gear lever is used to manipulate the engine according to the route taken by the driver. In our case, the driver is the user. rails controller come help us to manipulate the data to display this user desires.

controllers creation

The process for creating a controller is very easy, there is a syntax to write it manually or generate it. To generate a controller is just to type in the keyword “rails generate controller <ControllerName>”. Notice that you are capitalizing ControllerName and using the singular form. This is a Rails paradigm that you should follow each time you create a controller.

This command produces several tasks, including the following:

It creates a file called app / controllers / ControllerName_controller.rb. If you look inside you will find it as follows :

screenshot from vs code

class ControllerNameController <ApplicationController end”.

This allows us to be able to generate routes using the resources keyword. For example. resources: controllerNames.

Controller classes inherit from ApplicationController, which is the other file in the Controllers folder: application.rb. The ApplicationController contains code that can be run in all your controllers and it inherits from the Rails ActionController::Base class.

Implementing Methods in controllers

It is in the controllers that all methods of data management and security are implemented. Here in our example, we will implement the basic methods (CRUD), that describe the user interface.

a screenshot from vs code with empty methods

The index method gives you a list of all data from the table that is managed by this controller in the database. This functionality will be achieved by the codes implemented inside this method as shown below.

The show method displays only further details on simple data from a table. To achieve this, you just type one line of code.

Once you take user input using HTML form, it is time to create a record in the database. To achieve this, you are called to insert codes in the create method in the controllerNames_controller.rb to fill with the code provided here below in the file attached.

The method update will be called after the edit method when the user modifies a data and wants to update the changes into the database. The update method is similar to the create method and will be used to update existing <dataName>in the database.

After creating <dataName>, list all datas or edit them, and delete a record from the database, you will use it. Implement this method as follows.

screenshot from vsCode

Thank you for taking the time to follow this guide. I will advise you to practice it on your computer. to learn more about other rail concepts, I suggest you take these courses.,external%20requests%20to%20internal%20actions.