DEV Community

Cover image for My first rails scaffold
Saral Karki
Saral Karki

Posted on

My first rails scaffold

I finished my first app last week. This app basically is my personal blog. I hope to maintain and add features to it in the days to come. Today though, I started with a new project - a todo list. The first thing I did for this project was rails g scaffold todo_list title:string description:text. And with it, I had used the scaffold for the first time. There was one thing I wanted to do though, I really wanted to understand how the scaffold worked.

Here's my understanding of how the scaffold works. Upon creating the new scaffold with the above command, it did a couple of things for me.

  1. It generated the routes for me in the routes.rb file in the config folder. Viewing the routes.rb file in my editor, I could see resources :todo_list on the file.

  2. It generated a model TodoList and once after migrating the database via 'rails db:migrate` the database was ready to be used. The model had a title and description because I had set those two parameters when creating the scaffold.

  3. Scaffold also generated all the controllers for the CRUD methods. Meaning from the get-go I would be able to perform create, read, update and deletion of entries.

  4. Finally, all the views were generated, and the views had a form with title and description. Also since the controllers had already defined the CRUD methods, I was able to create, delete, edit and see my entries.

It was that easy. My entire last week's work summarised by one syntax. 😵😵😵😵

Still, last week was a great learning experience for me building everything from scratch, and this week as I delve into the scaffold generated code, I will really read between the lines of codes and understand what it does.

Top comments (1)

lennythedev profile image
Lenmor Ld

Nice post!
I find that Rails make it easy to "bootstrap" things, but what makes it challenging for me, coming from Node and Python, are the assumptions and things work out-of-the-box which gets hard to trace
Having scaffolded "Item"
The route /items and /items.json just work without any code to render the page/json explicitly (maybe i'm wrong about this)

Keep us posted on your journey to learn Rails :)