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:
-
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-levelC:\
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
-
Then download the course code bundle and unzip it into your
rails_ii_studio
directory. You'll end up with a directory calledpragstudio-rails5-ii-code
. Inside that directory you'll find three directories:example-app
,exercise-solutions
, andstarter_app
. -
Inside the
starter_app
directory, find the directory calledflix
. Copy thatflix
directory to yourrails_ii_studio
directory. You want to end up with arails_ii_studio/flix
directory that contains a copy of the baseline app. -
Then change into your
flix
application directory.Mac OS X or Linux:
cd ~/rails_ii_studio/flix
Windows:
cd \rails_ii_studio\flix
-
The
flix
application directory includes aGemfile
that lists all the gems necessary to run the app. Install the gems by typingbundle install
-
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 therails
command.rake db:migrate db:seed
-
As a quick smoke test, go ahead and run all the specs:
rspec
They should all pass!
-
Now you're ready to fire up the server:
rails s
-
Then navigate on over to http://localhost:3000 and you should see a list of example movies with poster images.
-
Go ahead and click on a movie and write a review!
-
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:
-
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. -
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. -
Those controllers access the database via two models in the
app/models
directory:Movie
andReview
. 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. -
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, thelayouts/application.html.erb
file uses partials to create a consistent layout divided into header, main content, sidebar, footer, and flash sections. -
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 theimages
subdirectory and all the CSS stylesheets in thestylesheets
directory. We used Sass fairly heavily to make writing CSS a little sweeter with variables, functions, nesting, and so on. -
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 ranrails db:seed
earlier. If you have some favorite movies, feel free to add them to this file. Then to reseed the data, runrails db:reset
. This task drops and re-creates the database, applies all the migrations, and populates the database with the seed data indb/seeds.rb
. -
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 ranrails db:migrate
earlier. -
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 ranrspec
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!