What are we building?
Let's learn Rails by building a simple web application. Let's name it granite
.
Granite is a
type of rock.
The application will be a simple todos for a company of small size. The
application will have the following features.
- Users will be able to prioritize the tasks by starring them. These starred
tasks will be shown first in the list.
- Users can toggle the task as pending or done.
- Task creators can edit or delete the task.
- Users can add comments to the tasks. These comments cannot be edited or deleted.
Our goal is to learn the process of writing a Ruby on Rails application with
React.js.
Live application
Here's the working demo of the
application.
Use the following credentials to login:
If the above credentials don't work, then create a sign-up with a new user from
the UI and login with those new credentials.
Setting up a new Rails application
Let's build a brand new Rails application. Rails offers us a command-line
utility to build a brand new Rails application.
Run this command to generate a new Rails application with a specific Rails
version:
It will produce a long output and might take some time to complete. It is
downloading and setting up required files from the internet. Once it's finished, you can navigate to the root directory of your newly created granite
project.
We are aware that the latest version of Rails is 7.1.2. We will be updating the book soon.
Whenever we update the book we need to ensure that the whole thing works fine. Hence it's taking some time.
Configuring default Ruby and Node.js versions
We can lock our application with a specific Ruby and Node.js version. This will
help us to isolate the dependencies of Ruby and Node.js modules used in the
application.
In our application, we will use Node.js version 18.12
and Ruby version 3.2.2
.
Run the following commands from the terminal:
These files will be read by rbenv
and nvm
to auto-set the versions.
Now run the following commands from the terminal to ensure that our project is
currently using these specified versions:
The above commands should ideally point out that the specified versions are
already installed and you can safely exit. If that's not the case, then please
go ahead with the installation of the required versions, using the same
commands.
Fixing rexml gem issue
We are not going to use some of the auto-generated gems for testing. Thus let's
remove those unwanted gems, among which there are selenium
based gems that may
cause rexml
dependency issue.
Run the following command from the terminal:
The above command should've removed those unwanted gems from the Gemfile
.
Remove unwanted gems
Some gems come pre-installed in Rails 7 but are not required for our Granite
application like importmap-rails
, turbo-rails
, and stimulus-rails
.
Remove the following lines, including the comments, from the Gemfile
:
Then update the Gemfile.lock
file by running the following command:
Updating .gitignore file
We don't need all of the generated files to be version-controlled. To ignore
these unwanted files, we specify them in the .gitignore
file. BigBinary has
specified a standard set of folders and files to be ignored by git in the
Wheel repository.
Run the following command from the terminal, in the root of the project, to use
that config:
Now the .gitignore
file should be visible in your project's root.
Removing fixtures
In a future chapter, we will see why we are removing fixtures. For now, let's
just go ahead and disable the fixtures.
To disable using fixtures open test/test_helper.rb
and comment out the line
shown below.
Execute the following lines on the terminal to delete the fixtures folder
completely:
Ensure fixtures won't get generated
Rails provide several generator commands that help in generating boilerplate
code for models, controllers, migration scripts, etc. We will be using them
throughout the book.
But, with some of those generators, fixture files also get generated by default.
We won't need those fixture files.
Fortunately, we can customize Rails generators to disable the auto-generation of
fixtures for all the commands.
To do that, add the following lines in config/application.rb
:
For simple applications, we can always rely on using ActiveRecord
queries to
create seed data for tests instead of fixtures. We will learn about them in the
upcoming chapters.
Creating database
SQlite3 database is the default database
for a newly created Rails application.
Run this command to create databases for our application:
The output will look like this:
Starting the server
Now let's start the server. Run this command:
The output would look like this:
Open the browser and visit http://localhost:3000. We
will see a page that would look like this:
Verify Ruby and NodeJS versions from the project root
Before committing the files, let's verify whether our Ruby and Node.js versions
are the required versions or not.
For starters ensure that you will be running the next set of commands from the root
of the newly created granite project. We can run the following UNIX command from
the terminal to verify what path we are currently at:
If you are not in the granite
project directory, then
cd
into it.
Now run the following commands from the
terminal from your project's root directory:
Also from the root of the project run the following command to double-verify
that Ruby is set to the required version:
Another final point of verification is the version line in the Gemfile
, which
can be found at the root of the project. The following line with specified
version should be present within the Gemfile
:
If the Ruby version is not matching the rbenv
set version, then it means that
there is a system-level Ruby version that is taking precedence. In that case,
you'd have to set the Ruby version globally as mentioned in the
previous chapter.
Once verified, add this new application to git: