
Creating views
We will be using Rails' scaffold method to create models, views, and other necessary files that Rails needs to make our application live. Here's the set of tasks that our application should perform:
The task looks pretty lengthy, but any Rails developer would know how easy it is to do. We don't actually have to do anything to achieve it. We just have to pass a single scaffold command, and the rest will be taken care of.
Close the Rails server using Ctrl + C keys and then proceed as follows:
- First, navigate to the project folder in the terminal. Then, pass the following command:
rails g scaffold todo title:string description:text completed:boolean
This will create a new model called
todo
that has various fields like title, description, and completed. Each field has a type associated with it. - Since we have created a new model, it has to be reflected in the database. So, let's migrate it:
rake db:create db:migrate
The preceding code will create a new table inside a new database with the associated fields.
- Let's analyze what we have done. The scaffold command has created many HTML pages or views that are needed for managing the
todo
model. So, let's check out our application. We need to start our server again:rails s
- Go to the localhost page
http://localhost:3000
at port number3000
. - You should still see the Rails' default page. Now, type the URL:
http://localhost:3000/todos
. - You should now see the application, as shown in the following screenshot:
- Click on New Todo, you will be taken to a form which allows you to fill out various fields that we had earlier created. Let's create our first todo and click on submit. It will be shown on the listing page:
It was easy, wasn't it? We haven't done anything yet. That's the power of Rails, which people are crazy about.