Ruby on Rails is a web framework that makes it easy to build powerful web apps in a short amount of time.
Ruby on Rails is written in the Ruby programming language.
1.1.In the terminal, type
$ rails new MySite
1.2. Then run$ bundle install
1.3.Finally type
$ rails server
2.Hello Rails II
In three commands, you built a working Rails app that displays the Rails default page. How does it work?
- The
rails new
command created a new Rails app named MySite. It generated a number of files and folders that we will use to build the app. In the Code Editor, click on the folder icon to see these files and folders. We'll see what these files and folders are for in the next exercises. Therails new
command is the starting point of every Rails project. - The
bundle install
command installed all the software packages needed by the new Rails app. These software packages are called gems and they are listed in the file Gemfile. - The
rails server
command started the Rails development server so that we could preview the app in the browser by visiting http://localhost:8000. This development server is called WEBrick.
What happens when you visit http://localhost:8000 in the browser? Check out the diagram in the browser.
- The browser makes a request for the URL http://localhost:8000.
- The request hits the Rails router in config/routes.rb. The router recognizes the URL and sends the request to the controller.
- The controller receives the request and processes it.
- The controller passes the request to the view.
- The view renders the page as HTML.
- The controller sends the HTML back to the browser for you to see.
3.1.Looking at the request/response cycle, we need three parts to build a Rails app: a controller, a route, and a view. Let's start here by creating a controller.
In the terminal, type:
$rails generate controller Pages
3.2.After rails generate
finishes running, in the Code Editor, open app/controllers/pages_controller.rb. Within the class PagesController
, add a method home
:
class PagesController < ApplicationController
def home
end
end
4.Routes
We created a new controller named Pages. How did we do this?
6.Generalizations - The
rails generate controller Pages
command generated a new controller named Pages. This created a file named app/controllers/pages_controller.rb. - Inside the new Pages controller, we added a method called
home
. Methods in Rails controllers are also referred to as controller actions, so here we added thehome
action to the Pages controller.
4.1.Now that we have a controller, let's move on to the second part of the request/response cycle and create a route.
Open config/routes.rb and underneath line 1, type:
Open config/routes.rb and underneath line 1, type:
get 'welcome' => 'pages#home'
5.Views
Now when a user visits
5.1.Now that we have a controller and a route, let's move on to the third part of the request/response cycle and create a view.
Open app/views/pages/home.html.erb, and type in the following HTML. Fill in your own name. <div class="main">
<div class="container">
<h1> Hello my name is Becky</h1>
<p>I make Rails apps.</p>
</div>
</div>
http://localhost:8000/welcome
, the route
get 'welcome' => 'pages#home'
will tell Rails to send this request to the Pages controller's home
action.5.1.Now that we have a controller and a route, let's move on to the third part of the request/response cycle and create a view.
Open app/views/pages/home.html.erb, and type in the following HTML. Fill in your own name. <div class="main">
<div class="container">
<h1> Hello my name is Becky</h1>
<p>I make Rails apps.</p>
</div>
</div>
We've provided CSS in the file app/assets/stylesheets/pages.css.scss.
You built a Rails app from scratch. What can we generalize so far?
Using the request/response cycle as a guide, this has been our workflow when making a Rails app.
- Generate a new Rails app.
- Generate a controller and add an action.
- Create a route that maps a URL to the controller action.
- Create a view with HTML and CSS.
- Run the local web server and preview the app in the browser.