You are on page 1of 6

CodeCademy Ruby On Rails

Getting Started
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.

The rails new command allows you to create a new Rails app and name it. It generates a number
of files and folders that we will use to build the app. The rails new command is the starting point of
every Rails project.

rails new MySite # can name the app anything you want

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.

bundle install

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.
Localhost is the loopback-address of your pc.

rails server

When developing a Rails app, the request/response cycle is a useful guide to see how all the
app’s files and folders fit together. The request/response cycle traces how a user’s request flows
through the app. Understanding the request/response cycle is helpful to figure out which files to
edit when developing an app and where to look when things aren’t working.

1. A user opens his browser, types in a URL and presses Enter.


2. When a user presses Enter, the browser makes a request for that URL.
3. The request hits the Rails router (config/routes.rb). The router maps the URL to the correct
controller and action to handle the request.
4. The action receives the request, processes it and passes it on to the view.
5. The view renders the page as HTML
6. The controller sends the HTML back to the browser. The page loads and the user sees it.

In this way, the request/response cycle is a useful way to see how a Rails app’s files and folders
are for and how they fit together.

The rails generate controller command generates a new controller which is named. This creates a
file named app/controllers/name_controller.rb.

rails generate controller Pages

inside the new Pages controller, we added a method called home. Methods in Rails controllers are
also referred to as controller actions, so we added the home action to the Pages controller.

We’ve built a Rails app containing a static page. To do this we used a controller, a route and a
view.
Now we create a route by opening config/routes.rb. We type this in

get ‘welcome’ => ‘pages#home’

This tells Rails to send this request to the Pages controller’s home action. Syntax:

get ‘url’ => ‘controller_name#action’


Saving Data
However, a Rails app with static pages looks the same for all users. We can use a database to
help us create apps that save information.

Here’s how a database fits into a request-response cycle:

1. A user opens his browser, types in a URL, and presses Enter. When a user presses Enter, the
browser makes a request for that URL.
2. The request hits the Rails router (config/routes.rb).
3. The router maps the URL to the correct controller and action to handle the request
4. The action receives the request and asks the model to fetch data from the database.
5. The model returns a list of data to the controller action.
6. The controller action passes the data onto the view.
7. The view renders the page as HTML
8. The controller sends the HTML back to the browser. The page loads and the use sees it.

From the request/response cycle, we need four parts to build a Rails app, a model, a route, a
controller, and a view.

The rails generate model command creates a new model, that can be named.

rails generate model Message # creates a new model called Message

In doing so, Rails created two files:

1. A model file in app/models/message.rb. The model represents a table in the database.


2. A migration file in db/migrate. Migrations are a way to update the database.

The migration file contains a few things:

class CreateMessages < ActiveRecord::Migration


def change
create_table :messages do |t|
t.text :content
t.timestamps
end
end
end

1. The change method tells Ruby what change to make to the database. Here it uses the
create_table method to create a new table In the database for storing messages.
2. Inside create_table, we added t.text :content. This will create a text column called content in
the messages tables.
3. The final line t.timestamps is a Rails command that creates two more columns in the messages
table called created_at and updated_at. These columns are automatically set when a message
is created and updated.

The rake db:migrate command updates the database with the new messages data model.

The rake db:seed command seeds the database with sample data from db/seeds.rb.
Adding a route

get ‘/messages’ => ‘messages#index’

Adding an index action in the controller named Messages,


(app/controllers/messages_controller.rb):

def index
@messages = Message.all
end

Now we have added a controller and a route to the Rails app

Rails provides seven standard controller actions for doing common operations with data. Here we
want to display a list of all messages, so we use the index action.

What happens is when a use visits http://localhost:8000/messages, the routes file maps this
request to the Messages controller’s index action. The index action retrieves all messages from
the database and stores them in variable @messages.

Ruby on Rails defines seven standard controller actins that can be used to do common things
such as display and modify data:

If you want to create routes for all seven actions on your app, you can add a resource route to
your routes file. This resource route below maps URLs to the controller’s seven actions.

resources :messages
if you only want to create specific routes for specific actions, you can use :only to fine tune the
resource route. This line maps URLs only to the Message controller’s index and show actions.

resources :messages, only: [:index, :show]

The file index.html.erb is a web template. Web templates are HTML files that contain variables and
control flow statements. We can use web templates to loop through and display data from the
database.

<% @messages.each do |message| %> # iterates through each message in


<div class="message"> # the @messages array from index action
<p class="content"><%= message.content %></p> # display content
<p class="time"><%= message.created_at %></p> # display time created
</div>
<% end %>

The default web templating language in Rails is embedded Ruby or ERB.

Generalizations:

 A model represents a table in the database


 A migration is a way to update the database with a new table, or changes to an existing table.
 Rails provides seven standard controller actions for doing common things such as display and
create data
 Data can be displayed in the view using ERB web templating
 Data can be saved into the database using a web form.
Associations 1
has_many :destinations denotes that a single Tag can have multiple Destinations

belongs_to :tag denotes that each destination belongs to a single tag

the has_many / belongs_to pair is frequently used to define one-to-many relationships.

Can add more columns to models

class CreateTags < ActiveRecord::Migration


def change
create_table :tags do |t|
t.string :title
t.string :image
t.timestamps
end
end
end

12/03/2019, resetting progress as don’t understand

You might also like