38
Building Web Apps with Rails II

RoR 101: Session 2

Embed Size (px)

DESCRIPTION

Part 2 of 6

Citation preview

Page 1: RoR 101: Session 2

Building Web Apps with Rails

II

Page 2: RoR 101: Session 2

Recap

Page 3: RoR 101: Session 2

Hello Ruby!

Hello Rails

Page 4: RoR 101: Session 2

Try Ruby!

Http://tryruby.org

Stop when you see "summary 4" or stuff

about file operations.

Skip to challenge on Classes.

Page 5: RoR 101: Session 2

Our app: FirstFM

Page 6: RoR 101: Session 2

Scaffold

rails generate scaffold Station name:string description:text url:string

Page 7: RoR 101: Session 2

Scaffold

rails generate scaffold Station name:string description:text url:string

●Model●View●Controller●Database Migration

Page 8: RoR 101: Session 2

Anatomy of a Rails App

Models, Views, & Controllers

Migrations Files

Page 9: RoR 101: Session 2

● Create – new, create

● Read – index, show

● Update – edit, update

● Destroy - destroy

CRUD actions in the controller

Page 10: RoR 101: Session 2

● Rails Console● ActiveRecord Queries

● Views● Embedded Ruby Syntax● Layouts● Helper Methods

Session 2

Page 11: RoR 101: Session 2

Controllers and Models

DBModels Controllers

Page 12: RoR 101: Session 2

Controllers and Models

DBModels Controllers

In stations_controller.rb (index):

@stations = Station.all

Page 13: RoR 101: Session 2

Controllers and Models

DBModels Controllers

In stations_controller.rb (show):

@stations = Station.find(params[:id])

Page 14: RoR 101: Session 2

Controllers and Models

DBModels Controllers

ActiveRecord::BaseMaps station model to stations table in database

Page 15: RoR 101: Session 2

The Rails Console

1. Open the terminal 2. Navigate to the 'firstfm' directory3. Run the commands:

rails console@stations = Station.all

Page 16: RoR 101: Session 2

The Rails Console

rails console@stations = Station.all

Recreate the first line of the show action.

1. Create a hash with the key :id 2. Pass it into Station.find()

Task!

Go to guides.rubyonrails.org/active_record_querying.html

Page 17: RoR 101: Session 2

The Rails Console

Recreate the first line of the show action.

1. Create a hash with the key :id 2. Pass it into Station.find()

Try out other AR methods:

guides.rubyonrails.org/active_record_querying.html

apidock.com/rails/ActiveRecord/Base

Page 18: RoR 101: Session 2

DBModels Controllers

Views

@station = Station.find(params[:id])

Page 19: RoR 101: Session 2

DBModels Controllers

Views

Views

@station = Station.find(params[:id])

Page 20: RoR 101: Session 2

DBModels Controllers

Views

Views

respond_to do |format| format.html # index.html.erb format.json {render json: @stations }end

Page 21: RoR 101: Session 2
Page 22: RoR 101: Session 2

View files match action names

Page 23: RoR 101: Session 2

Where are Create, update & destroy?

Page 24: RoR 101: Session 2

These are .erb, embedded ruby files

Page 25: RoR 101: Session 2

Let's Take A Look!

Page 26: RoR 101: Session 2

...

<% @stations.each do |station| %>

#loop stuff

<% end %>

...

index.html.erb:

<% %> for code

Page 27: RoR 101: Session 2

<p>  <b>Name:</b>  <%= @station.name %></p>

<p>  <b>Description:</b>  <%= @station.description %></p>

<p>  <b>Url:</b>  <%= @station.url %></p>

...

show.html.erb:

<%= %> for printing

Page 28: RoR 101: Session 2

<%= link_to 'Edit', edit_station_path(@station) %> |<%= link_to 'Back', stations_path %>

show.html.erb:

What's this?

Page 29: RoR 101: Session 2

<%= link_to 'Edit', edit_station_path(@station) %> |<%= link_to 'Back', stations_path %>

show.html.erb:

These are both view helpers.

Page 30: RoR 101: Session 2

View Helpers & Being DRY

Don't Repeat Yourself

Page 31: RoR 101: Session 2

View Helpers & Being DRY

View Helpers:

Methods we can call in our views to write less code / markup!

Page 32: RoR 101: Session 2

View Helpers & Being DRY

Rails Provides Us With Helpers:

●Links●Forms● Images●And even more!

Page 33: RoR 101: Session 2

Layout files are found here

Layouts

Page 34: RoR 101: Session 2

<!DOCTYPE html><html><head> <title>Firstfm</title> <%= stylesheet_link_tag "application" %> <%= javascript_include_tag "application" %> <%= csrf_meta_tags %></head><body>

<%= yield %>

</body></html>

The default layouttemplate.

Page 35: RoR 101: Session 2

<!DOCTYPE html><html><head> <title>Firstfm</title> <%= stylesheet_link_tag "application" %> <%= javascript_include_tag "application" %> <%= csrf_meta_tags %></head><body>

<%= yield %>

</body></html>

This is where our templates

are rendered

Page 36: RoR 101: Session 2

<!DOCTYPE html><html><head> <title>Firstfm</title> <%= stylesheet_link_tag "application" %> <%= javascript_include_tag "application" %> <%= csrf_meta_tags %></head><body>

<%= yield %>

</body></html>

This helpers include Our default js / css

Page 37: RoR 101: Session 2

Task: Display a logo image on each page !

1. Find and download an image to use as a logo

2. Put it in 'firstfm/public/assets/'

3. Display it with the 'image_tag' helper

Search for 'image_tag' on

http://apidock.com/rails for reference!

Page 38: RoR 101: Session 2

DB

Web Browser

Models

Routing SystemRails

Controllers

Views

How does it all fit together?