Set up Rails Controller

Tanuka Das
2 min readDec 23, 2019

The controller does the heavy lifting when the interaction happens between the model, server and user. It handles all the complex logic, communication, saving things to the database, rendering partials, giving feedback to the user, etc.

Step 1: Generate the controller

That creates tones of files, one of the most important being, app/controllers/hats_controller.rb, the hat's controller. It has a class called HatsController and it inherits from the application controller. All the actions, methods for this controller will go between the class declaration and the end, at the bottom.

Step 2: Define the routes

Add this in the config/routes.rb file. This defined resources, hats, create a whole bunch of possible addresses we can use inside of our browser; if you run rails routes on the console, it will display all of those possible addresses that can be used to build the application.

Step 3: Open the server

This will lead to the rails http://localhost:3000/ browser, which is rails welcome page and if you go to the http://localhost:3000/hats it will show you a missing template error hats/index.

Congratulations! Now you can perform the full CRUD actions with your hats controller.

--

--

Tanuka Das

Software Developer, technical writer, bookworm, constant learner.