The Pragmatic Studio

Setup

Exercises

Objective

Welcome to your first exercise! We're delighted to have you along for the course.

This exercise will help you get everything set up and tested on your machine so you're ready to start writing code in the next exercise!

1. Install Ruby and Rails

If you've taken the Ruby on Rails: Level I course, then you already have Ruby and Rails installed.

If you're jumping straight into this course, then this likely isn't your first Rails rodeo and you're already set up with Rails.

If for any reason you need a fresh install of Ruby and Rails, check out our installation instructions. We'll wait right here for you...

2. Choose a Code Editor

We trust you already have a favorite editor at the ready for coding up Rails apps. If not, we recommend using Sublime Text 2 as we do in the videos. Sublime Text runs equally well on Mac, Windows, or Linux. And because it's natively compiled for each platform, it's fast! You can try out an unlicensed, fully-featured version at no cost for as long as you like. And if you end up liking Sublime Text as much as we do, it's well worth supporting the development by buying a licensed copy for $70.

You might want to go ahead and download our Sublime Text Shortcuts PDF (Mac, Windows) for reference as you work through the exercises. It's a cheat sheet of the most common keyboard shortcuts you may see us use in the videos.

3. Prepare the Starter App

This course picks up where we left off at the end of the Ruby on Rails: Level I course. As such, in the exercises you'll be extending the final version of the movie-review app we built in the previous course.

Here's how to get a copy of the baseline flix app up and running:

  1. Start by creating a directory called rails_ii_studio to keep all the code for this course.

    If you're running Mac OS X or Linux, create the rails_ii_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_ii_studio
    cd ~/rails_ii_studio

    If you're running Windows, create the rails_ii_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 three commands:

    cd c:\
    mkdir \rails_ii_studio
    cd \rails_ii_studio
  2. Then download the course code bundle and unzip it into your rails_ii_studio directory. You'll end up with a directory called pragstudio-rails5-ii-code. Inside that directory you'll find three directories: example-app, exercise-solutions, and starter_app.

  3. Inside the starter_app directory, find the directory called flix. Copy that flix directory to your rails_ii_studio directory. You want to end up with a rails_ii_studio/flix directory that contains a copy of the baseline app.

  4. Then change into your flix application directory.

    Mac OS X or Linux:

    cd ~/rails_ii_studio/flix

    Windows:

    cd \rails_ii_studio\flix
  5. The flix application directory includes a Gemfile that lists all the gems necessary to run the app. Install the gems by typing

    bundle install
  6. Then prepare the database by running the migrations and seeding the example data:

    rails db:migrate db:seed

    Rails 4: You must use the rake command rather than the rails command.

    rake db:migrate db:seed
  7. As a quick smoke test, go ahead and run all the specs:

    rspec

    They should all pass!

  8. Now you're ready to fire up the server:

    rails s
  9. Then navigate on over to http://localhost:3000 and you should see a list of example movies with poster images.

  10. Go ahead and click on a movie and write a review!

  11. While you're playing around with the app, feel free to add a new movie or edit an existing movie. You can find example movie info on IMDb or Wikipedia, for example. You'll need to put new movie image files in the app/assets/images directory.

4. Review the Application Code

Throughout this course we'll be building upon the existing application code, so spend a few minutes reviewing the flix app code in your favorite editor. As all Rails apps share a common directory structure, you'll find everything in the familiar places.

Here are a few areas to explore in particular:

  1. In the config/routes.rb file you'll see that the application manages two resources: movies and reviews. Reviews are always accessed in the context of a particular movie, so the reviews resource is nested inside the movies resource. To get a quick lay of the land in terms of generated routes, jump over to http://localhost:3000/rails/info/routes.

  2. These routes map to two controllers in the app/controllers directory. MoviesController defines the seven conventional actions for creating, reading, updating, and deleting movies. ReviewsController has a subset of actions for creating and listing reviews, always in the context of a particular movie.

  3. Those controllers access the database via two models in the app/models directory: Movie and Review. A movie can have many reviews and a review belongs to a single movie. So the models have a one-to-many relationship. And of course we've declared some reasonable validations.

  4. The app/views directory contains your typical view templates. There's nothing out of the ordinary here, but it's worth a quick look. You'll notice that we use a partial for rendering forms and validation errors. As well, the layouts/application.html.erb file uses partials to create a consistent layout divided into header, main content, sidebar, footer, and flash sections.

  5. All the web design stuff lives in the app/assets directory so it's served through the asset pipeline. You'll find all the images (including movie poster images) in the images subdirectory and all the CSS stylesheets in the stylesheets directory. We used Sass fairly heavily to make writing CSS a little sweeter with variables, functions, nesting, and so on.

  6. To bring some life to the app right out of the box, the db/seeds.rb file creates an interesting set of movies and reviews. This file was executed when you ran rails db:seed earlier. If you have some favorite movies, feel free to add them to this file. Then to reseed the data, run rails db:reset. This task drops and re-creates the database, applies all the migrations, and populates the database with the seed data in db/seeds.rb.

  7. The database schema evolved over a series of migrations which you'll find in the db/migrations directory. All these migrations were applied when you ran rails db:migrate earlier.

  8. And it wouldn't be a respectable app if it didn't come with automated tests. So in the spec directory you'll find both model and features specs written using RSpec and Capybara. All these specs where executed when you ran rspec earlier.

Now that you have everything installed, you know your way around the app, and all the specs pass, we're ready to start adding new features!

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.