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 25, 2024
1 parent cc9d0b9 commit e9fce40
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 60 deletions.
123 changes: 63 additions & 60 deletions guides/source/getting_started.md
Expand Up @@ -7,8 +7,7 @@ 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 to create, set up, and run a 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 +71,72 @@ 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.

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
This guide helps you get set up with [Developer Containers (or Dev Containers for short)](https://containers.dev/)
for a full-featured development environment. Dev Containers are used to run 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 your Rails application up and running.

#### 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
```
Alternatively, to install Rails directly on your machine, you can read through the
[Installing Rails guide](installing-rails.html)

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.
### Setup and Installation

To install Rails on Windows, you'll first need to install [Ruby Installer](https://rubyinstaller.org/).
To get set up, you will need to install the relevant tools; Docker, VSCode and
`rails-new`. We'll go into detail about each one below.

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, an open platform for developing, shipping, and
running applications. 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, launch the Docker Application to begin running
the Docker engine on your machine.

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

```bash
$ sqlite3 --version
```
Visual Studio Code (VSCode) is an open source code editor developed by Microsoft. VSCode's Dev Container
extension allows you to open any folder inside (or mounted into) a container and take advantage of
Visual Studio Code's full feature set. A [devcontainer.json](https://code.visualstudio.com/docs/devcontainers/containers#_create-a-devcontainerjson-file)
file in your project tells VS Code how to access (or create) a development container with a
well-defined tool and runtime stack. It allows you to quickly spin up containers, access terminal
commands, debug code, and utilize extensions.

The program should report its version.
You can install VSCode by downloading it from [the website](https://code.visualstudio.com/).

#### Installing Rails
You can install the Dev Container extension by downloading it from [the marketplace](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers).

To install Rails, use the `gem install` command provided by RubyGems:
#### Installing rails-new

```bash
$ gem install rails
```
`rails-new` generates a new Rails application for you without having to install Ruby on
your machine. It uses Docker to generate the Rails application, thus allowing Docker to
take care of installing the correct Ruby and Rails versions for you.

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.
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.

NOTE: The examples below use `$` to represent your terminal prompt in a UNIX-like OS,
though it may have been customized to appear differently.

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 +173,30 @@ 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

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. Start by launching VSCode
and opening your application.

Once the application opens, VSCode should prompt you that a it has found a Dev Container
configuration file, and you can reopen the folder in a Dev Container. Click the green "Reopen
in Container" button to create the Dev Container.

Once the Dev Container setup is complete, your development environment is ready to use,
with Ruby, Rails, and all your dependencies installed.

You can open the terminal within VScode to verify that Rails is installed:

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

For the remainder of this guide, you will be working within VSCode which serves
as your entry point for your application that will run in 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 e9fce40

Please sign in to comment.