Remember that learning Ruby is a continuous process
You still have many resources, exercises and projects to work on in Ruby
We will be moving our focus over to Rails now, but it is important that you keep looking to improve your Ruby skills
Many of your skills will improve naturally with experience
Rails is a framework for building web applications. Before we jump into Rails, let's take a moment to better understand how web applications work in the internet.
How does the internet work?
What happens when I type google.com in my browser?
What happens when I search for something in google?
How does my computer connect to Google's computers?
Requesting a web page
Clients
Things that view the web
Web Browsers
Mobile Browsers
Servers
Where web pages are stored and "served"
You give it a command, and it gives you something back
Servers store all the data for web applications, and serve them in the format the client requests (e.g. HTML, JSON)
Servers are computers, just like clients (but typically much more powerful)
Rails
Rails is a Web Application Framework written in Ruby.
Takes in parameters from the server
Processes the request
Generates HTML/CSS (your code)
Sends back to the server
Why Rails?
Rails is an MVC framework
Rails was first released by DHH in 2004
Rails has gained lots of popularity as an easy-to-use framework for prototyping apps
Rails is one of the best frameworks for testing & TDD
Rails has evolved a lot over the past decade and made it incredibly easy to do many of the common web app goals (e.g. building APIs, forms, mail, etc.)
Why Not Rails?
Steep learning curve
The Rails Way or the highway
Abstractions make poor design decisions easier
The Double-Edged Sword
"Convention over Configuration" -Rails Mantra
Learning Rails: Critical Goals
How to design your application
How to implement your design using Rails
BREAK
Create An App
$ gem install rails -v 4.2.1$ cd ~/ixperience/d6
# creates a new folder 'cinema' inside current folder# with all the files rails needs to run$ rails new cinema
Explore The App Structure
$ cd cinema
$ ls
Open the app in your editor
$ cd cinema
$ sublime .
Start the server!
$ rails server# shortcut: rails s
Rails is alive!
Once the server is running, you can access it at localhost:3000
The default port that it runs on is 3000
By default, it shows the simple landing index page
Using Rails as a Static Server
Try modifying the index page to be different
Try adding your own pages and accessing them (static .HTML files)
Try adding images to /public/ and accessing them
This is the same functionality we used in our github.io pages.
The Next Level
Now, we want to go the next step and start building dynamic web applications, using Rails scaffolding functionality.
What is a dynamic web application?
Dynamic web applications are different from static web sites in that they change and respond based on user interactions. Pretty much any interactive web site you have visited is almost surely powered by a dynamic web application server (e.g. Google, Gmail, Netflix, Twitter).
These apps listen to your requests (a Google search, a Netflix movie lookup) and respond accordingly. We will be building applications that respond to requests made by users.
Rails Scaffolding
Scaffolds automatically give us the basic functionality for creating, deleting, and editing simple resources in our application. This is also known as "CRUD".
Resources
The building blocks of our application.
Simple Examples of Resources
Users are resources in almost every single application.
For Amazon: Products
For Netflix: Movies
For Twitter: Tweets
Recap
Rails is a bunch of Ruby code that we will use to make our interactive web site
To use Rails, we will write a small amount of Ruby, but place it into appropriate 'slots' inside the Rails app to make it work
We will use the Rails Scaffolding tool to make it easier to get started
Scaffolding
$ rails generate scaffold Movie title:stringyear:string
Run the server again
If we now hit the server again, we get a 'ActiveRecord::PendingMigrationError' error. This means that your migrations have not run yet.
Migrations are mechanisms for making changes to the database. We won't try to fully understand this yet, but we'll fix it...
Run migrations
$ rake db:migrate
Voila
Navigate to localhost:3000/movies to see your scaffolded Movies functionality.
We've just built our first Rails app. Nice!
Scaffolding
Within seconds, Rails scaffolding provided us with a pretty incredible set of barebones functionality. This is a great start.
We won't be using Scaffolding for much longer, but it's a good way to hit the ground running when you're just getting started.