50
Building Web Apps with Rails III

RoR 101: Session 3

Embed Size (px)

DESCRIPTION

Part 3 of 6

Citation preview

Page 1: RoR 101: Session 3

Building Web Apps with Rails

III

Page 2: RoR 101: Session 3

● Rails Console● ActiveRecord Queries

● Views● Embedded Ruby Syntax● Layouts● Helper Methods

Recap

Page 3: RoR 101: Session 3

Recap: ActiveRecord

DBModels Controllers

In stations_controller.rb (show):

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

Page 4: RoR 101: Session 3

The Rails Console

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

rails console@stations = Station.all

Page 5: RoR 101: Session 3

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

show.html.erb:

These are both view helpers.

Page 6: RoR 101: Session 3

<!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 7: RoR 101: Session 3

DB

Web Browser

Models

Routing SystemRails

Controllers

Views

Session 3

Page 8: RoR 101: Session 3

DB

Web Browser

Models

Routing SystemRails

Controllers

Views

Session 3: Routing

Page 9: RoR 101: Session 3

DB

Web Browser

Models

Routing SystemRails

Controllers

Views

Session 3: Validations & Testing

Page 10: RoR 101: Session 3

DB

Web Browser

Models

Routing SystemRails

Controllers

Views

Session 3: Associations& The 'Stream' Model

Page 11: RoR 101: Session 3

Routing in Rails

Page 12: RoR 101: Session 3

Routing in Rails

●Which controller to instantiate

●Which action to invoke

●Parameter(s) to pass

Web Browser

?

Page 13: RoR 101: Session 3

Routing in Rails

●Follows routing rules defined in:

●config/routes.rb

Web Browser

Routing System

Controllers

Views

Page 14: RoR 101: Session 3

Config/Routes.rb:

Firstfm::Application.routes.draw do resources :stations #commentsend

Routing in Rails

Generates RESTful routes for the 'Station' resource

Page 15: RoR 101: Session 3

Config/Routes.rb:

Firstfm::Application.routes.draw do resources :stations #commentsend

Routing in Rails

Representational State Transfer Principles:

Use HTTP methods explicitlyBe stateless

Expose directory structure-like URIsHandle multiple formats e.g. XML, JSON

Page 16: RoR 101: Session 3

Config/Routes.rb:

Firstfm::Application.routes.draw do resources :stations #commentsend

Routing in Rails

This means we can acceptrequests for all the CRUD

actions

Page 17: RoR 101: Session 3

Routing in Rails: Terminal Time!

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

rake routes

Page 18: RoR 101: Session 3

Routing in Rails: Writing Rules

Config/Routes.rb:

resources :stations

Page 19: RoR 101: Session 3

Routing in Rails: Writing Rules

Config/Routes.rb (rewritten):

get 'statations' => 'stations#index'get 'statations/:id' => 'stations#show'get 'statations/new' => 'stations#new'post 'statations' => 'stations#create'put 'statations/:id' => 'stations#update'delete 'statations/:id' => 'stations#destroy'

Page 20: RoR 101: Session 3

Routing in Rails: Writing Rules

Config/Routes.rb:

resources :stations

Generates URL helpers for us e.g.

stations_pathstation_path(:id)new_station_pathEtc...

Page 21: RoR 101: Session 3

Routing in Rails: Task

1. Define a root controller and action

2. Add a new action

2.1 Write a new method in the controller2.2 Write a view file for it2.3 Write a routing rule so we can access it

For docs on routing: guides.rubyonrails.org/routing.html

Page 22: RoR 101: Session 3

Tests and Validations

Page 23: RoR 101: Session 3

We have a problem...

Page 24: RoR 101: Session 3

Our app accepts empty station data

Page 25: RoR 101: Session 3

Solution:

Write validations to prevent erroneous data

Tests and Validations

Page 26: RoR 101: Session 3

But First!

Let's write tests.

Tests and Validations

Page 27: RoR 101: Session 3

Tests and Validations

Scaffolding has generated testfiles for us

Page 28: RoR 101: Session 3

require 'test_helper'

class StationTest < ActiveSupport::TestCase  # test "the truth" do  #   assert true  # endend

In firstfm/test/unit/station_test.rb:

We write tests by writing out 'assertions'

Tests and Validations: Unit Testing

Page 29: RoR 101: Session 3

require 'test_helper'

class StationTest < ActiveSupport::TestCase  test "the truth" do     assert 10 > 9  endend

Tests and Validations: Unit Testing

'assertions' evaluate whether an object is what we expect it to be

Page 30: RoR 101: Session 3

Tests and Validations: Unit Testing

ruby -Itest <script_name>

Running test scripts

ruby -Itest test/unit/station_test.rb

e.g.

Page 31: RoR 101: Session 3

Tests and Validations: Unit Testing

Script report example:

ruby -Itest test/unit/station_test.rb

pass: 1, fail: 1, error: 0total: 2 tests with 2 assertions in 0.215168805 seconds

Page 32: RoR 101: Session 3

test "shouldnt save without name" dostation = Station.newstation.url = “http://myradio.com”station.description = “A cool radio 

station”assert !station.save

end

Tests and Validations: Unit Testing

Page 33: RoR 101: Session 3

test "should save" dostation = Station.newStation.name = “My Radio”station.url = “http://myradio.com”station.description = “A cool radio 

station”assert station.save

end

Tests and Validations: Unit Testing

Page 34: RoR 101: Session 3

Tests and Validations: Task!

Write test cases for your validations

Consider more than just presence of data e.g.

● Name shouldn't be longer than x characters

● URL should start with 'http://'

● Description shouldn't be less than x characters

Page 35: RoR 101: Session 3

Tests and Validations: Validate

● We write validation code in the model

● Rails provides us with several built in validation methods

● For example: 'validates_presence_of'

Page 36: RoR 101: Session 3

Tests and Validations: Validate

Example: Using validates_presence_of In models/station.rb:

class Station < ActiveRecord::Base

validates_presence_of :name, :url, :description

end

Page 37: RoR 101: Session 3

Tests and Validations: Task!

Make your tests pass by implementing validations like below

class Station < ActiveRecord::Base

validates_presence_of :name, :url, :description

end

Page 38: RoR 101: Session 3

Associations in RoRAdding 'Streams'

Page 39: RoR 101: Session 3

Station

First FM

Page 40: RoR 101: Session 3

Station Stream

First FM

Page 41: RoR 101: Session 3

Station Streams

First FM

Has many

Page 42: RoR 101: Session 3

First FM

Let's generate the stream model

rails generate model stream url:string name:string

Create the db table by running the migration script...

rake db:migrate

Page 43: RoR 101: Session 3

First FM

Let's check out our new stream model

rails c

mystream = Stream.new

Page 44: RoR 101: Session 3

Station Stream

First FM

Page 45: RoR 101: Session 3

Station Streams

First FM

Has many

Page 46: RoR 101: Session 3

Station Streams

First FM

Has many

has_many :streams

belongs_to :station

Page 47: RoR 101: Session 3

First FM

Let's see if our models are associated

rails c

mystation = Station.first

mystation.streams

Page 48: RoR 101: Session 3

First FM

OH NOES!

We need to add a secondary key.

rails generate migration AddStationIdToStreams station_id:integer

Page 49: RoR 101: Session 3

First FM

OH YEA!

We've got a secondary key!

rake db:migrate

rails c

mystation = Station.first

mystation.streams

Page 50: RoR 101: Session 3

First FM

Build

Build and save a new stream

stream = mystation.streams.build