The Pragmatic Studio

Create the App

Exercises

Objective

Now that we have Rails installed, it's time to start creating a Rails app!

In this exercise we'll create a new Rails app called flix. It's a super-short exercise just to get everything up and running. We'll incrementally add features to the flix app throughout subsequent exercises.

1. Generate the Skeleton App

All Rails apps have a common application directory structure and a set of supporting files. Creating all the directories and files by hand would be really tedious, not to mention being prone to error. Instead, with a single command we can generate a working Rails app. We often refer to this as a skeleton app because it's a bare-bones app that we then customize by putting some meat on the bones. Bone appétit!

  1. Start by creating a directory named rails-studio to hold the app you'll create while taking this course. Then change into that directory. The commands to do that are the same regardless of which operating system you're using, but the directory structure is slightly different.

    If you're running Mac OS X or Linux, create the rails-studio directory in your home directory (represented by the tilde character). To create the directory and change into it, type the following two commands:

    mkdir ~/rails-studio
    cd ~/rails-studio

    If you're running Windows, create the rails-studio directory in the top-level C:\ directory (represented by the backslash character) since Windows doesn't really have the concept of a user's home directory. To create the directory and change into it, type the following two commands:

    cd c:\
    mkdir \rails-studio
    cd \rails-studio
  2. Next, use the rails command to generate a new Rails app called flix:

    rails _7.0.8_ new flix

    Note: The _7.0.8_ part makes sure that the correct version of Rails is used to generate the app, in the event that you have multiple versions of Rails installed.

    Running this command will generate all the standard Rails directories and supporting files inside of a new flix directory. All the required gems listed in the Gemfile will be automatically installed, as well.

  3. Go ahead and change into the new flix directory:

    cd flix
  4. Then open that directory in your code editor. For example, if you're using Visual Studio Code then you can either open the directory using the File -> Open... menu item or from the command line by typing:

    code .

    Have a quick look around at what got generated, just to get your bearings. There's a lot of stuff in there, but don't let it throw you. We'll visit each directory as needed throughout the course.

2. Run the App

The Rails application generator is kind enough to generate a working application, which means we can run it without having to first fuss around with configuration.

  1. Inside the flix application directory, start your new Rails app by typing:

    rails server

    Or, if you're the type of person who loves to show off your power moves around the office, you can use the shortcut:

    rails s

    This command starts a web server and you should see output that looks something like this:

    => Booting Puma
    => Rails 7.0.8 application starting in development
    => Run `bin/rails server --help` for more startup options
    Puma starting in single mode...
    * Puma version: 5.6.7 (ruby 3.1.4-p223) ("Birdie's Version")
    *  Min threads: 5
    *  Max threads: 5
    *  Environment: development
    *          PID: 32727
    * Listening on http://127.0.0.1:3000
    * Listening on http://[::1]:3000
    Use Ctrl-C to stop

    Now you have a web server listening on port 3000 of your local machine.

  2. Next, open your favorite web browser and browse to the URL http://localhost:3000. You should see the default Rails welcome page. Yay!

    As a skeleton app this app doesn't do much (yet), but it's always good to fire up a newly-generated app and make sure everything works before customizing it.

  3. Now would be a really good time to check the information on the default Rails page to verify that you're running the correct versions of Ruby and Rails. :-)

  4. Finally, leave the app running in your current Terminal or command prompt window, and open a new session. That way you can type the commands in future exercises without having to stop and restart the server. Don't forget to change into the flix application directory in your new session.

Solution

All the exercise solutions for the movie-review app you're writing in the exercises, as well as the code for the events app we're writing in the videos, is available for download in the code bundle file (right-click to save).

When you unzip the file, you'll end up with a directory named pragstudio-rails7-code. Inside that directory you'll find the following directories:

  • eventz contains the events app we build in the videos. The code is organized into directories matching the course modules. For example, the create-app directory contains a snapshot of how the eventz code looks at the end of the "Create the App" video.

  • flix contains the application you build in the exercises. The code is organized into directories matching the course modules. For example, after completing the exercise for the "Create the App" module, your code should roughly match the code in the create-app directory.

  • prepared-files contains files we've prepared for you to copy into the project as needed later on in the course to save tedious typing.

The full solution for this exercise is in the create-app directory of the code bundle.

Bonus Round

Some folks just aren't satisfied to gloss over all those generated files and directories. We know, it's just too tempting to go digging around in there. So if you're feeling curious, go ahead and get familiar with what got generated. Here's a rundown of what's in each directory:

  • app is where you'll put the bulk of your application code. This is where you'll spend most of your time. Rails puts a lot of emphasis on keeping code organized, so the app directory has a number of subdirectories:

    • models, views, and controllers are appropriately-named directories where you'll organize your (wait for it) model, view, and controller code.

    • assets contains sub-directories where you'll store your application's static assets such as images and stylesheets.

    • channels is where you put Ruby classes for handling real-time features using Action Cable.

    • helpers is where you put Ruby modules that define utility methods for views.

    • javascript is where you put JavaScript modules that get compiled by Webpack.

    • jobs is where you put Ruby classes for running background jobs.

    • mailers is where you put Ruby classes for generating and sending e-mails.

  • bin isn't something you'll mess with. It contains the rails script file and other scripts.

  • config is where you go to tweak the configuration settings of your application. Initially you won't have to touch most of the files in here because generated Rails apps are configured with "sensible defaults". Later in this course we'll make stops at the following key places:

    • database.yml configures the database used by each environment.

    • routes.rb maps incoming requests (URLs) to application code.

    • environments is a directory that contains three files that define specific settings for each environment: development, test, and production.

    • initializers is a directory where you put Ruby code that needs to be run when the application starts.

  • db contains everything related to the database, including migration files and, in the case of using the default SQLite3 database, the database file itself.

  • lib is where you put any reusable "library" code that's not a model, view, or controller. It's more common these days to package this sort of code as a gem. But it has one important subdirectory:

    • tasks is where you would put any custom Rake tasks for your application, with each file having a .rake extension.

  • log is where Rails automatically creates log files so you have a record of what happened when your app ran. Each environment gets its own log file.

  • public is the document "root" directory of the app. It contains static files that are served directly by the web server. For example, 404.html lives here and gets served when a page can't be found.

  • storage is where Active Storage stores uploaded files when running in the development environment.

  • test is where you put test files if you use the default testing library.

  • tmp is where Rails stores any temporary files needed by the app. We can't recall the last time we even peeked in this directory.

  • vendor is where we put third-party code that's not packaged as a gem that's declared in the Gemfile.

  • Last, but by no means least, the Gemfile file in the top-level directory contains a list of third-party dependencies (Ruby gems) that your application needs.

See, it wasn't too intimidating after all. :-)

Wrap Up

Pat yourself on the back—you just created your first Rails app! Now you're ready to start customizing it with your own code. Onward and upward to the next section!

All course material, including videos and source code, is copyrighted and licensed for individual use only. You may make copies for your own personal use (e.g. on your laptop, on your iPad, on your backup drive). However, you may not transfer ownership or share the material with other people. We make no guarantees that the source code is fit for any purpose. Course material may not be used to create training material, courses, books, and the like. Please support us by encouraging others to purchase their own copies. Thank you!

Copyright © 2005–2024, The Pragmatic Studio. All Rights Reserved.

Ruby on Rails 7

Rails 7 Edition

0% complete