Skip to content

Commit

Permalink
Update Getting Start With Rails guide to account for Dev Containers
Browse files Browse the repository at this point in the history
This commit updates the Getting Started With Rails guide to cover using rails-new, Docker and VScode to create and run a new app in a Dev Container.

The old instructions for installing Ruby and Rails locally on the developer's machine have been moved to their own guide, Installing Rials.
  • Loading branch information
andrewn617 committed Apr 22, 2024
1 parent cc9d0b9 commit 02f3c90
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 61 deletions.
125 changes: 64 additions & 61 deletions guides/source/getting_started.md
Expand Up @@ -7,8 +7,8 @@ This guide covers getting up and running with Ruby on Rails.

After reading this guide, you will know:

* How to install Rails, create a new Rails application, and connect your
application to a database.
* How create a new Rails application
* How to work with your Rails application in a Dev Container
* The general layout of a Rails application.
* The basic principles of MVC (Model, View, Controller) and RESTful design.
* How to quickly generate the starting pieces of a Rails application.
Expand Down Expand Up @@ -72,92 +72,63 @@ By following along with this guide, you'll create a Rails project called
`blog`, a (very) simple weblog. Before you can start building the application,
you need to make sure that you have Rails itself installed.

This guide makes use of Dev Containers, which allow you to work with your Rails
application in a container, without needing to install Rails or its dependencies
directly on your machine. This is the fastest way to get started with Rails.

If you would like to install Rails directly on your machine, you can see the
[Installing Rails guide](installing-rails.html)

NOTE: The examples below use `$` to represent your terminal prompt in a UNIX-like OS,
though it may have been customized to appear differently. If you are using Windows,
your prompt will look something like `C:\source_code>`.

### Installing Rails

Before you install Rails, you should check to make sure that your system has the
proper prerequisites installed. These include:

* Ruby
* SQLite3

#### Installing Ruby

Open up a command line prompt. On macOS open Terminal.app; on Windows choose
"Run" from your Start menu and type `cmd.exe`. Any commands prefaced with a
dollar sign `$` should be run in the command line. Verify that you have a
current version of Ruby installed:

```bash
$ ruby --version
ruby 3.1.0
```

Rails requires Ruby version 3.1.0 or later. It is preferred to use the latest Ruby version.
If the version number returned is less than that number (such as 2.3.7, or 1.8.7),
you'll need to install a fresh copy of Ruby.
### Installing rails-new

To install Rails on Windows, you'll first need to install [Ruby Installer](https://rubyinstaller.org/).
This guide utilizes the `rails-new` tool to create a Rails app on your machine. We will
then run our application in a Dev Container using VSCode. Both rails-new and the
Dev Container run in a containerized environment using Docker. So we will need to install
VSCode and Docker as well as `rails-new`.

For more installation methods for most Operating Systems take a look at
[ruby-lang.org](https://www.ruby-lang.org/en/documentation/installation/).
#### Installing Docker

#### Installing SQLite3
Dev Containers are run using Docker. You can install Docker by following the
installation instructions for your operating system in the [Docker docs](https://docs.docker.com/desktop/).

You will also need an installation of the SQLite3 database.
Many popular UNIX-like OSes ship with an acceptable version of SQLite3.
Others can find installation instructions at the [SQLite3 website](https://www.sqlite.org).
Once Docker has been installed, open it from your application folder to begin running
the Docker engine on your machine.

Verify that it is correctly installed and in your load `PATH`:

```bash
$ sqlite3 --version
```

The program should report its version.

#### Installing Rails

To install Rails, use the `gem install` command provided by RubyGems:

```bash
$ gem install rails
```
#### Installing VSCode

To verify that you have everything installed correctly, you should be able to
run the following in a new terminal:
The easiest way of working with Dev Containers is using the VSCode editor. You can
install VSCode by downloading it from [the website](https://code.visualstudio.com/).

```bash
$ rails --version
Rails 7.2.0
```
#### Installing rails-new

If it says something like "Rails 7.2.0", you are ready to continue.
`rails-new` is a tool which can generate a new Rails application without needing
Ruby or Rails installed on your machine. To install rails-new, follow the installation
instructions [in the readme](https://github.com/rails/rails-new?tab=readme-ov-file#installation).

### Creating the Blog Application

Rails comes with a number of scripts called generators that are designed to make
your development life easier by creating everything that's 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.
you don't have to write it yourself. The `rails-new` tool uses this generator to
create a new Rails application for you.

To use this generator, open a terminal, navigate to a directory where you have
To use `rails-new` to generate your app, open a terminal, navigate to a directory where you have
rights to create files, and run:

```bash
$ rails new blog
$ rails-new blog
```

This will create a Rails application called Blog in a `blog` directory and
install the gem dependencies that are already mentioned in `Gemfile` using
`bundle install`.
This will create a Rails application called Blog in a `blog` directory.

TIP: You can see all of the command line options that the Rails application
generator accepts by running `rails new --help`.
generator accepts by running `rails-new --help`.

After you create the blog application, switch to its folder:

Expand Down Expand Up @@ -194,6 +165,38 @@ of the files and folders that Rails creates by default:
|.gitignore|This file tells git which files (or patterns) it should ignore. See [GitHub - Ignoring files](https://help.github.com/articles/ignoring-files) for more information about ignoring files.|
|.rubocop.yml|This file contains the configuration for RuboCop.|
|.ruby-version|This file contains the default Ruby version.|
|.devcontainer/|This folder contains the Dev Container configuration|

### Opening the Blog Application in a Dev Container

A Dev Container is a fully functional development environment running in a container
on your machine. Our new Rails application comes with a Dev Container already
configured and ready to use.

We will use VSCode to spin up and work with our Dev Container. First we need to open
our app in VSCode. This can be done by opening VSCode and then opening our application's
folder, or directly from the command line within our application's directory with:

```bash
$ code .
```

When we open the application, VSCode will recognize that a Dev Container is available
and will prompt us to re-open the application in a Dev Container. Click the prompt
and the Dev Container will begin initializing.

Once the Dev Container setup is complete, VSCode will now be working with a fully
setup development environment, with Ruby, Rails and all gems and dependencies installed.

You can open the terminal within VScode to verify:

```bash
$ rails --version
Rails 7.2.0
```

For the remainder of this guide, you will be working from the terminal in VSCode, which
is your entry point for working your Application running inside the Dev Container.

Hello, Rails!
-------------
Expand Down
112 changes: 112 additions & 0 deletions guides/source/installing_rails.md
@@ -0,0 +1,112 @@
**DO NOT READ THIS FILE ON GITHUB, GUIDES ARE PUBLISHED ON https://guides.rubyonrails.org.**

Installing Rails
================

After reading this guide, you will know:

* How to install Rails
* How to create a Rails application

--------------------------------------------------------------------------------

This guide is for those who would like to install Rails on their local machine.
See [Getting Started](getting_started.html) for the full guide to getting started
with Rails, including working with Rails in a Dev Container, which is the quickest
way to get started.

The best way to read this guide is to follow it step by step. All steps are
essential to run this example application and no additional code or steps are
needed.

NOTE: The examples below use `$` to represent your terminal prompt in a UNIX-like OS,
though it may have been customized to appear differently. If you are using Windows,
your prompt will look something like `C:\source_code>`.

Installing dependencies
-----------------------

Before you install Rails, you should check to make sure that your system has the
proper prerequisites installed. These include:

* Ruby
* SQLite3

### Installing Ruby

Open up a command line prompt. On macOS open Terminal.app; on Windows choose
"Run" from your Start menu and type `cmd.exe`. Any commands prefaced with a
dollar sign `$` should be run in the command line. Verify that you have a
current version of Ruby installed:

```bash
$ ruby --version
ruby 3.1.0
```

Rails requires Ruby version 3.1.0 or later. It is preferred to use the latest Ruby version.
If the version number returned is less than that number (such as 2.3.7, or 1.8.7),
you'll need to install a fresh copy of Ruby.

To install Rails on Windows, you'll first need to install [Ruby Installer](https://rubyinstaller.org/).

For more installation methods for most Operating Systems take a look at
[ruby-lang.org](https://www.ruby-lang.org/en/documentation/installation/).

### Installing SQLite3

You will also need an installation of the SQLite3 database.
Many popular UNIX-like OSes ship with an acceptable version of SQLite3.
Others can find installation instructions at the [SQLite3 website](https://www.sqlite.org).

Verify that it is correctly installed and in your load `PATH`:

```bash
$ sqlite3 --version
```

The program should report its version.

### Installing Rails

To install Rails, use the `gem install` command provided by RubyGems:

```bash
$ gem install rails
```

To verify that you have everything installed correctly, you should be able to
run the following in a new terminal:

```bash
$ rails --version
Rails 7.2.0
```

If it says something like "Rails 7.2.0", you are ready to continue.

Creating an Application
-----------------------

To create a new Rails application, open a terminal, navigate to a directory where you have
rights to create files, and run:

```bash
$ rails new blog
```

This will create a Rails application called Blog in a `blog` directory and
install the gem dependencies that are already mentioned in `Gemfile` using
`bundle install`.

TIP: You can see all of the command line options that the Rails application
generator accepts by running `rails new --help`.

After you create the blog application, switch to its folder:

```bash
$ cd blog
```

Now you are ready to continue with the Getting Started guide, continuing from
the [Hello, Rails!](getting-started.html#hello-rails-bang) section.

0 comments on commit 02f3c90

Please sign in to comment.