Lesson 1
Setting Up a New Rails App
Setting Up Our Rails Application

Welcome to the first unit of our Ruby on Rails Basics course! Here, we'll take the first step in building our ToDo app by exploring the Rails application structure. Before we dive into coding and creating features, it's essential to understand the environment and project structure. Basic knowledge of the Ruby programming language is a prerequisite for this course and it is the only prerequisite. Once this foundation is laid, you’ll be well-prepared to develop more complex functionalities in future lessons.

What You'll Learn

In this unit, you'll get familiar with the structure of a new Rails application. A Rails application is a web application built using Ruby on Rails, a framework that helps you develop web apps quickly and easily. Ruby on Rails follows the Model-View-Controller (MVC) architecture, which is a way of organizing your code to separate different responsibilities.

Don't worry if these terms are new to you — we will cover each one as we go along. For now, we will focus on exploring the essential files and folders generated by Rails, such as config/application.rb, config/routes.rb, the Gemfile, and config/database.yml. Understanding the purpose and functionality of these files is crucial for navigating and managing your Rails project effectively.

Starting a New Rails Application

To create a new Rails application, you'll use the following command in your terminal:

Bash
1rails new todo_app

This command generates a new Rails application with the name todo_app, setting up the necessary files and folders. You will see several files and directories being created, such as app, config, db, Gemfile, and more.

Once the generation is complete, you can navigate into your application directory with:

Bash
1cd todo_app

From here, you can start exploring the project structure and configuration files. From now on, we will assume that these basic steps are done and will explore the generated files.

To start the Rails server, you can use:

Bash
1rails server

By default, your app will be accessible at URL http://localhost:3000.

The `config/application.rb` File

The config/application.rb file is the heart of your Rails application configuration. Here, you'll set up various configurations for your application, such as enabling or disabling certain frameworks, setting time zones, and configuring other environment-specific settings.

Here's a snippet of what the config/application.rb file looks like:

Ruby
1require_relative 'boot' 2 3require 'rails/all' 4 5Bundler.require(*Rails.groups) 6 7module TodoApp 8 class Application < Rails::Application 9 config.load_defaults 6.1 10 11 # Set the time zone 12 config.time_zone = 'Pacific Time (US & Canada)' 13 14 # Set the default locale to English 15 config.i18n.default_locale = :en 16 17 # Enable the asset pipeline 18 config.assets.enabled = true 19 end 20end

This script sets up and configures the Rails application by loading the default settings, specifying the Rails version, setting the time zone to Pacific Time (US & Canada) (this ensures your application handles time uniformly), establishing English as the default locale, and enabling the asset pipeline for managing JavaScript and CSS files (this helps managing assets effectively).

The `config/routes.rb` File

The config/routes.rb file is where you define the routes for your application. Routes are the URLs that users can access in your application. They determine which controller actions are invoked when a user visits a particular URL.

We'll set up the basic routing in config/routes.rb like this:

Ruby
1Rails.application.routes.draw do 2 root 'todos#index' 3 resources :todos, only: [:index, :show] 4end

This routing configuration creates two endpoints: one for a list of todos (index) and another for showing a specific todo (show).

Breaking Down the Example:

  1. root 'todos#index': This line specifies the root route of your application. When users visit the root URL (e.g., http://localhost:3000/, not http://localhost:3000/todos or http://localhost:3000/a/b/c/d), Rails directs them to the index action.

  2. resources :todos, only: [:index, :show]: The resources method generates routes for the todos resource. By specifying only: [:index, :show], we limit the generated routes to just the index and show actions. This corresponds to:

    • The URL /todos which maps to the index action, typically used to display a list of todos.
    • The URL /todos/:id which maps to the show action, used to display a specific todo based on its ID.
Conventional Action Names

While defining routes, Rails uses conventional names that follow the RESTful design pattern. Some of these actions include:

  • index: Used for displaying a list of resources.
  • show: Used for displaying a single resource.
  • new: Used for displaying a form for creating a new resource.
  • create: Used for handling the submission of a form to create a new resource.
  • edit: Used for displaying a form for editing an existing resource.
  • update: Used for handling the submission of a form to update an existing resource.
  • destroy: Used for deleting a resource.

These conventional names help maintain consistency across Rails applications, making them easier to understand and maintain.

The `Gemfile`

The Gemfile is where you list all the dependencies your Rails application needs. These dependencies are Ruby libraries (gems) that provide additional functionality.

Here’s a small section of what a typical Gemfile might look like:

Ruby
1source 'https://rubygems.org' 2gem 'rails', '~> 6.1.3' 3gem 'pg', '>= 0.18', '< 2.0' 4gem 'puma', '~> 5.0'

This file not only lists the necessary gems but also specifies the versions to ensure compatibility.

To install these dependencies, you'll use the Bundler tool by running:

Bash
1bundle install
The `config/database.yml` File

The config/database.yml file is where you configure the database settings for your Rails application. This file contains information about the type of database you're using, connections for different environments (development, test, production), and other database-related configurations.

Here’s an example of what the config/database.yml file might look like:

YAML
1default: &default 2 adapter: postgresql 3 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 4 timeout: 5000 5 6development: 7 <<: *default 8 database: todo_app_development 9 10test: 11 <<: *default 12 database: todo_app_test 13 14production: 15 <<: *default 16 database: todo_app_production 17 username: todo_app 18 password: <%= ENV['TODO_APP_DATABASE_PASSWORD'] %>

In this configuration, we're using PostgreSQL as our database adapter and specifying different databases for development, test, and production environments. The production section includes environment variables for the username and password to enhance security.

Why It Matters

Getting your Rails app set up correctly is like laying the foundation for a house. A strong foundation makes everything else more stable and easier to build upon. By understanding the initial setup, you'll be in a strong position to navigate your project and add new features efficiently. Familiarity with the project structure and essential configuration files will also help you troubleshoot issues and incorporate best practices as your application grows.

Ready to get started? Let’s move to the practice section and explore our pre-configured Rails application together. This will set you up for success in building a robust ToDo app step by step.

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.