Getting Started with Rails: The Basics Every Developer Should Know

dou.eu 5 dni temu

In this article, I want to go through the preparation for development work, installing all the necessary tools so you can begin your journey as a Ruby on Rails developer.

Terminal

Terminal or Command line prompt

For all installations use the command line prompt (terminal app), it will help you in the future to work with the production servers, because usually it’s a virtual machine where you will connect through the command line.

Depending on your operating system, you will have different ways to open the Terminal:

  • On the Mac OS or Linux open the Terminal app
  • on Windows choose "Run" from your Start menu and type cmd.exe

While terminal basics are outside the scope of this post, I recommend familiarizing yourself with a few essential commands:

  • pwd (Print Working Directory)
  • ls (List files and directories)
  • cd (Change directory)
  • sudo ( Super user do)

These are sufficient to get started. You can easily find tutorials online to learn these commands.

The built-in terminals have everything you need and can be customized as well. I use the Warp terminal, which has a modern look and a free built-in GPT-style assistant that can explain errors and provide hints. It looks like this:

Warp terminal

That could be helpful when you don’t know anything or forget something. You can try it with my referral link.


Code Editors

There are plenty of them, from free to paid but to start, I suggest using Visual Studio Code as the most popular, again according to the rails developer survey. It’s easy to customize with plugins, too. I might even dive into that in a future post since I’m using it myself right now. That said, when you’re just starting your programming journey, any editor will do the trick for editing files. Pick one that feels comfortable for you.


Rails installation

Let’s get everything you need to start building Rails applications. On the official Ruby on Rails Guide, you can find the detailed steps. Here I will list the main steps with some additional entry-level comments.

Install Ruby language

Check if you have a Ruby installed ( the $ sign is a prompt indicator; you don't need to type it; to execute the command — press "Enter"; below the result of the command):

$ ruby --version ruby 3.3.5 (2024-09-03 revision ef084cc8f4) [arm64-darwin24]

If you don’t have any Ruby installed or an older version, consider installing the latest available Ruby version. The best way to use the Ruby version manager, like rbenv, rvm, or asdf, as they are currently the most popular according to the survey.

Imagine you want to build 2 different applications locally, each app has different ruby versions, with the version manager you can easily install different versions to your OS and it will automatically switch to the specific one per application.

The full list of ruby install options you can find on ruby-lang.org.


Installing SQLite3

SQLite3 is a simple but powerful production-ready database.

Check if you have it installed in the terminal:

$ sqlite3 --version 3.43.2 2023-10-10 13:08:14 1b37c146ee9ebb7acd0160c0ab1fd11017a419fa8a3187386ed8cb32b709aapl (64-bit)

If you don’t have it find installation instructions on the SQLite3 website.

Installing Rails

It’s as easy as running:

$ gem install rails Fetching rails-8.0.0.gem Successfully installed rails-8.0.0 Parsing documentation for rails-8.0.0 Installing ri documentation for rails-8.0.0 Done installing documentation for rails after 0 seconds 1 gem installed

and after installation check if it was installed properly:

$ rails -v Rails 8.0.0


If you don’t have gem installed, and see something like"

$ gem -v bash: gem: command not found

You need to install the Gem plugin. The installation depends on your system, so it is better to google it.


Creating a new rails application

In this step, with only one command, Rails will create a new sample app with default configurations. Before running it, navigate to a directory where your "terminal user" has the right to create files. I’ll use the Documents/ruby_apps/ folder. The cd command (change directory) will do the trick:

$ cd Documents/ruby_apps/

Now let's create the rails application:

$ rails new store Rails Generators

Rails comes with several scripts called generators that are designed to make your development life easier by creating everything necessary to start working on a particular task. One of these is the new application generator, which will provide you with the foundation of a fresh Rails application so that you don't have to write it yourself.

The word "store" in the command is the name of your app, you can name it whatever you want (e.g., rails new mysuperapp). For the sake of simplicity while you go through the rails guide it’s better to keep it as it is, so you have the same code locally.

The whole structure is described in the rails guide, so better check there, but don’t worry you will get it in a while during the adding more logic into and adding the code to the certain places.

To make sure that all is good and running you need to go to the app directory:

$ cd store

and start the server with this command:

$ rails server

it’s the same as rails s or bin/rails server

$ rails s => Booting Puma => Rails 8.0.0 application starting in development => Run `bin/rails server --help` for more startup options Puma starting in single mode... * Puma version: 6.4.3 (ruby 3.3.5-p100) ("The Eagle of Durango") * Min threads: 3 * Max threads: 3 * Environment: development * PID: 72509 * Listening on http://127.0.0.1:3000 * Listening on http://[::1]:3000 Use Ctrl-C to stop

Now if you open http://127.0.0.1:3000 in the browser you should see the rails welcome page:

the default rails welcome page

Congratulations! The first step is the hardest one but you did great.

What to do next? Probably read through the rails guide and try all the other commands.

If something is raising the errors or doesn’t work like described, it could be because of the different ruby/rails versions or some other dependencies, but usually all the solutions are easy to google.


Managing Dependencies with Bundler

Speaking of versions, Bunder is a tool to control the rails and other gems versions (similar to the rbenv for ruby version manager).

Bundler vs bundle

The name of the tool is “Bundler”, and the command to use this tool is “bundle”.

A “gem” is a bunch of perfectly packaged Ruby code that gives you access to custom methods somebody else wrote.

What does it mean, and how do you use it? When you run the bundle install command, it looks into the application’s Gemfile, where all dependencies are mentioned. For example, in this new app, I have:

# Gemfile source "https://rubygems.org" gem "rails", "~> 8.0.0" gem "sqlite3", ">= 2.1" ... and more ...

The file structure is straightforward: each line typically includes a gem word, name, and the version of the library that needs to be installed. There are various ways to specify the version, whether it’s specific, a range, or even no version at all—it will install the latest stable version. After the bundler installation, an automatically generated Gemfile.lock file lists the full dependency structure of the application. For now, just remember that you don’t want to manually change the Gemfile.lock file. It functions like a system file. If you wish to change the version of a gem, add a new gem, or remove some, you should always make changes in the Gemfile or through the Bundler API if it’s a dependency.

So, all these dependencies are installed on your local machine in the specific ruby version folder. BTW, in the application, you have a ".ruby-version" file that helps your system to understand which ruby version to use for the app.



Stay tuned! In my upcoming posts I want to describe the MVC (Model-View-Controller) pattern and how to Build CRUD Applications Quickly with Rails.


Idź do oryginalnego materiału