Let's create a new Rails app named daily-habits, and launch it using the bin/dev
command. Note that I am using Tailwind CSS for styling the components.
$ rails new daily-habits --css=tailwind
$ cd daily-habits
$ bin/dev
We'll start by creating the core data model for our application: a Habit.
Step 1: Generate the Habit Model
The very first thing that we're going to do is to generate a model named Habit with two properties: name
and count
indicating the name of the habit and how many days you've followed that habit.
$ bin/rails generate model habit name:string count:integer
After running this command, Rails will generate a few files for you, including a database migration file. Let's run the database migration, so a habits
table is created in the database.
$ bin/rails db:migrate
Now open the Rails console and create a sample habit named 'Write Every Day', because writing is awesome and you all should write every day.
$ bin/rails console
> Habit.create(name: 'Write Every Day', count: 5)
That's it. Now we have some data to work with and display on the screen. Next, we'll set up the route where we'll access the above habit.