An Introduction to Rails g P o l l a c k t h G r e g w - - PowerPoint PPT Presentation

an introduction to rails
SMART_READER_LITE
LIVE PREVIEW

An Introduction to Rails g P o l l a c k t h G r e g w - - PowerPoint PPT Presentation

An Introduction to Rails g P o l l a c k t h G r e g w i Episode # 1 Prerequisites: TryRuby.org Episode #1 Deep in the CRUD F O FOR ZOMBIES!! R Z O M B I E S ! ! CRUD Columns { (we have 3) tweets Rows { (we have


slide-1
SLIDE 1

An Introduction to Rails

w i t h G r e g g P

  • l

l a c k

Episode #1

slide-2
SLIDE 2

Prerequisites:

TryRuby.org

slide-3
SLIDE 3

Episode #1 Deep in the CRUD

slide-4
SLIDE 4

F O R Z O M B I E S ! ! FOR ZOMBIES!!

slide-5
SLIDE 5

CRUD

tweets

Rows{

(we have 4)

Columns

{

(we have 3)

slide-6
SLIDE 6

id status zombie

Retrieve a hash of the tweet with id = 3

Zombie Challenge #1

CRUD

b = { :status => "I just ate some delicious brains", :zombie => "Jim" }

Result

slide-7
SLIDE 7

Hash

Series of key value pairs

puts b[:status] puts b[:zombie] puts b[:zombie] + " said " + b[:status]

CRUD

b = { :status => "I just ate some delicious brains", :zombie => "Jim" } "Jim said I just ate some delicious brains" "Jim" "I just ate some delicious brains"

slide-8
SLIDE 8

tweets Retrieve a hash of the tweet with id = 3

Zombie Challenge #1

id status zombie 1 Where can I get a good bite to eat? Ash 2 My left arm is missing, but I don't care. Bob 3 I just ate some delicious brains. Jim 4 OMG, my fingers turned green. #FML Ash

Answer

3 puts t[:id] t = Tweet.find(3) puts t[:status]

"I just ate some delicious brains."

puts t[:zombie] "Jim"

CRUD

slide-9
SLIDE 9

puts t.id puts t.status puts t.zombie

S a m e A s

CRUD

puts t[:id] t = Tweet.find(3) puts t[:status] puts t[:zombie]

Answer

3 puts t.id puts t.status

"I just ate some delicious brains."

puts t.zombie "Jim"

slide-10
SLIDE 10

CRUD

t = Tweet.find(3)

tweets

id status zombie 1 Where can I get a good bite to eat? Ash 2 My left arm is missing, but I don't care. Bob 3 I just ate some delicious brains. Jim 4 OMG, my fingers turned green. #FML Ash

Pluralize

slide-11
SLIDE 11

C R U D

slide-12
SLIDE 12

Create Read Update Delete

slide-13
SLIDE 13

Create Read Update Delete

Tweet.find(3) t = Tweet.find(3) t.zombie = "EyeballChomper" t.save t = Tweet.find(3) t.destroy t = Tweet.new t.status = "I <3 brains." t.save

slide-14
SLIDE 14

Create

Syntax

t = Tweet.new t.status = "I <3 brains." t.zombie = "Jim" t.save

Alternate Syntax

t = Tweet.new(:status => "I <3 brains", :zombie => "Jim") t.save Tweet.create(:status => "I <3 brains", :zombie => "Jim")

Notice we don’t set the id. The id gets set manually for us.

slide-15
SLIDE 15

Read

Syntax

method chaining

Tweet.find(2) # Returns a single item Tweet.find(3, 4, 5) # Returns an array Tweet.first # Returns the first tweet Tweet.last # Returns the last tweet Tweet.all # Returns all the tweets Tweet.order(:zombie) # All ordered by zombie Tweet.limit(10) # Only 10 tweets Tweet.where(:zombie => "ash") # Only tweets by Ash Tweet.where(:zombie => "ash").order(:zombie).limit(10) Tweet.count # Returns number of tweets

slide-16
SLIDE 16

Update

Syntax

t = Tweet.find(3) t.zombie = "EyeballChomper" t.save t = Tweet.find(2) t.attributes = { :status => "Can I munch your eyeballs?", :zombie => "EyeballChomper" } t.save t = Tweet.find(2) t.update_attributes( :status => "Can I munch your eyeballs?", :zombie => "EyeballChomper" )

slide-17
SLIDE 17

t = Tweet.find(2) t.destroy Tweet.find(2).destroy

Delete

Syntax

Tweet.destroy_all

slide-18
SLIDE 18

Zombie lab 1 Zombie lab 1

slide-19
SLIDE 19

An Introduction to Rails

w i t h G r e g g P

  • l

l a c k

Episode #2

slide-20
SLIDE 20

Chapter 2 Models taste like chicken

slide-21
SLIDE 21

t = Tweet.find(3)

tweets

id status zombie 1 Where can I get a good bite to eat? Ash 2 My left arm is missing, but I don't care. Bob 3 I just ate some delicious brains. Jim 4 OMG, my fingers turned green. #FML Ash

Tweet

?

class Tweet < ActiveRecord::Base end

app/models/tweet.rb

Models

slide-22
SLIDE 22

Models

tweets

id status zombie 1 Where can I get a good bite to eat? Ash 2 My left arm is missing, but I don't care. Bob 3 I just ate some delicious brains. Jim 4 OMG, my fingers turned green. #FML Ash

Tweet

class Tweet < ActiveRecord::Base end

app/models/tweet.rb

< ActiveRecord::Base

Maps the class to the table

slide-23
SLIDE 23

tweets

class Tweet < ActiveRecord::Base end

Tweet.find(3)

class

Tweet #3

t =

instance of Tweet

Models

app/models/tweet.rb

id status zombie 1 Where can I get a good bite to eat? Ash 2 My left arm is missing, but I don't care. Bob 3 I just ate some delicious brains. Jim 4 OMG, my fingers turned green. #FML Ash

slide-24
SLIDE 24

Validations?

id status zombie 1 Where can I get a good bite to eat? Ash 2 My left arm is missing, but I don't care. Bob 3 I just ate some delicious brains. Jim 4 OMG, my fingers turned green. #FML Ash

t = Tweet.new t.save

5

slide-25
SLIDE 25

Validations

class Tweet < ActiveRecord::Base end validates_presence_of :status

app/models/tweet.rb

> t = Tweet.new => #<Tweet id: nil, status: nil, zombie: nil> > t.save => false > t.errors => {:status=>["can't be blank"]} > t.errors[:status] => "can't be blank"

slide-26
SLIDE 26

Validations

validates_presence_of :status validates_numericality_of :fingers validates_uniqueness_of :toothmarks validates_confirmation_of :password validates_acceptance_of :zombification validates_length_of :password, :minimum => 3 validates_format_of :email, :with => /regex/i validates_inclusion_of :age, :in => 21..99 validates_exclusion_of :age, :in => 0...21, :message => “Sorry you must be over 21”

slide-27
SLIDE 27

Validations

validates :status, :presence => true validates :status, :length => { :minimum => 3 } validates :status, :presence => true, :length => { :minimum => 3 }

:presence => true :uniqueness => true :numericality => true :length => { :minimum => 0, :maximum => 2000 } :format => { :with => /.*/ } :inclusion => { :in => [1,2,3] } :exclusion => { :in => [1,2,3] } :acceptance => true :confirmation => true

Attribute Validation

slide-28
SLIDE 28

Relationships Relationships

Because they always travel in packs

slide-29
SLIDE 29

Relationships

tweets

id status zombie 1 Where can I get a good bite to eat? Ash 2 My left arm is missing, but I don't care. Bob 3 I just ate some delicious brains. Jim 4 OMG, my fingers turned green. #FML Ash

slide-30
SLIDE 30

Relationships

tweets

id status 1 Where can I get a good bite to eat? 2 My left arm is missing, but I don't care. 3 I just ate some delicious brains. 4 OMG, my fingers turned green. #FML

zombies

id name graveyard 1 Ash Glen Haven Memorial Cemetery 2 Bob Chapel Hill Cemetery 3 Jim My Father’s Basement

slide-31
SLIDE 31

Relationships

tweets

id status zombie_id 1 Where can I get a good bite to eat? 1 2 My left arm is missing, but I don't care. 2 3 I just ate some delicious brains. 3 4 OMG, my fingers turned green. #FML 1

zombies

id name graveyard 1 Ash Glen Haven Memorial Cemetery 2 Bob Chapel Hill Cemetery 3 Jim My Father’s Basement

slide-32
SLIDE 32

Relationships

5 Your eyelids taste like bacon. 2

tweets

id status zombie_id 1 Where can I get a good bite to eat? 1 2 My left arm is missing, but I don't care. 2 3 I just ate some delicious brains. 3 4 OMG, my fingers turned green. #FML 1

zombies

id name graveyard 1 Ash Glen Haven Memorial Cemetery 2 Bob Chapel Hill Cemetery 3 Jim My Father’s Basement

slide-33
SLIDE 33

A Tweet a Zombie belongs to

tweets

id status zombie_id 1 Where can I get a good bite to eat? 1 2 My left arm is missing, but I don't care. 2 3 I just ate some delicious brains. 3 4 OMG, my fingers turned green. #FML 1

zombies

id name graveyard 1 Ash Glen Haven Memorial Cemetery 2 Bob Chapel Hill Cemetery 3 Jim My Father’s Basement

class Tweet < ActiveRecord::Base end

app/models/tweet.rb

belongs_to :zombie class Zombie < ActiveRecord::Base end

app/models/zombie.rb

has_many :tweets

A Zombie Tweets has many

Singular Plural

slide-34
SLIDE 34

Relationships

> z = Zombie.find(2) => #<Zombie id: 2, name: "Bob", graveyard: "Chapel Hill Cemetery"> > t = Tweet.create(:status => "Your eyelids taste like bacon.", :zombie => z) => #<Tweet id: 5, status: "Your eyelids taste like bacon.", zombie_id: 2> > t.zombie => #<Zombie id: 2, name: "Bob", graveyard: "Chapel Hill Cemetery"> > t.zombie.name => "Bob" > ash = Zombie.find(1) => #<Zombie id: 1, name: "Ash", graveyard: "Glen Haven Cemetery"> > ash.tweets => [#<Tweet id: 1, status: "Where can I get a good bite to eat?", zombie_id: 1>, #<Tweet id: 4, status: "OMG, my fingers turned green. #FML", zombie_id: 1>] > ash.tweets.count => 4

A Tweet a Zombie belongs to

slide-35
SLIDE 35

Zombie lab 2 Zombie lab 2

slide-36
SLIDE 36

An Introduction to Rails

w i t h G r e g g P

  • l

l a c k

Episode #3

slide-37
SLIDE 37

Chapter 3 The View ain’t always pretty

I’m so gonna eat his brains

slide-38
SLIDE 38

Views Our Application Stack

4 Components Views Models

slide-39
SLIDE 39

app views zombie_twitter

Views

zombies tweets index.html.erb

Edible Rotting Bodies

show.html.erb

Embedded Ruby

List all tweets View a tweet

Ruby inside HTML

slide-40
SLIDE 40

<!DOCTYPE html> <html> <head><title>Twitter for Zombies</title></head> <body> <img src="/images/twitter.png" /> </body></html> <% tweet = Tweet.find(1) %> <h1><%= tweet.status %></h1> <p>Posted by <%= tweet.zombie.name %></p>

/app/views/tweets/show.html.erb

Show a tweet

FYI, This code sucks a little..

(for 2 reasons)

<% ... %> <%= ... %>

Evaluate Ruby

Eval and print result

slide-41
SLIDE 41

/app/views/layouts/application.html.erb

<!DOCTYPE html> <html> <head><title>Twitter for Zombies</title></head> <body> <img src="/images/twitter.png" /> </body></html> <% tweet = Tweet.find(1) %> <h1><%= tweet.status %></h1> <p>Posted by <%= tweet.zombie.name %></p>

/app/views/tweets/show.html.erb

<%= yield %>

Show a tweet

slide-42
SLIDE 42

application.html.erb app views zombie_twitter

Views

zombies tweets index.html.erb show.html.erb

List all tweets View a tweet

layouts

The main layout

slide-43
SLIDE 43

/app/views/layouts/application.html.erb

Additional Layout Components

</head> <body> <img src="/images/twitter.png" />

<%= yield %>

</body></html> <!DOCTYPE html> <html> <head> <title>Twitter for Zombies</title>

slide-44
SLIDE 44

</head> <body> <img src="/images/twitter.png" />

<%= yield %>

</body></html> <!DOCTYPE html> <html> <head> <title>Twitter for Zombies</title>

<%= stylesheet_link_tag :all %> <%= javascript_include_tag :defaults %> <%= csrf_meta_tag %>

/app/views/layouts/application.html.erb

Additional Layout Components

slide-45
SLIDE 45

<%= stylesheet_link_tag :all %> <%= javascript_include_tag :defaults %> <%= csrf_meta_tag %>

public stylesheets zombie_twitter

Include all stylesheets

<link href="/stylesheets/scaffold.css" media="screen" rel="stylesheet" type="text/css" />

Renders

Additional Layout Components

slide-46
SLIDE 46

<%= stylesheet_link_tag :all %> <%= javascript_include_tag :defaults %> <%= csrf_meta_tag %>

Include default JS

<script src="/javascripts/prototype.js" type="text/javascript"></script> <script src="/javascripts/effects.js" type="text/javascript"></script> <script src="/javascripts/dragdrop.js" type="text/javascript"></script> <script src="/javascripts/controls.js" type="text/javascript"></script> <script src="/javascripts/rails.js" type="text/javascript"></script> <script src="/javascripts/application.js" type="text/javascript"></script>

Prototype Javascript Framework

public javascripts zombie_twitter

Renders

Additional Layout Components

slide-47
SLIDE 47

<%= stylesheet_link_tag :all %> <%= javascript_include_tag :defaults %> <%= csrf_meta_tag %> < f

  • r

m t a r g e t = " h t t p : / / y

  • u

r s i t e . c

  • m

" >

Z

  • m

b i e H a c k e r S i t e

Your Site

<meta name="csrf-param" content="authenticity_token"/> <meta name="csrf-token" content="I+d..jI="/>

Automatically adds this to forms

Think comment spam Renders

Additional Layout Components

slide-48
SLIDE 48

Root path and images

http://ZombieTwitter.com/[something]

I f e x i s t s i n / p u b l i c u s e i t ,

  • t

h e r w i s e g

  • t
  • R

a i l s

<img src="/images/twitter.png" />

public stylesheets zombie_twitter javascripts images

Example

slide-49
SLIDE 49

, zombie_path(tweet.zombie)

... <p>Posted by <%= tweet.zombie.name %></p>

/app/views/tweets/show.html.erb

Adding a Link

Make zombie a link

<%= link_to tweet.zombie.name %>

Link Text Link Path (URL)

<a href="/zombies/1">Ash</a>

Renders

<%= link_to tweet.zombie.name, tweet.zombie %>

We can also write as

Link Text Object to Show

slide-50
SLIDE 50

link_to

Method

What options can you use with it?

  • 1. Look in the source

git clone http://github.com/rails/rails.git cd rails

Command Line

Open your editor and search for “def link_to”

slide-51
SLIDE 51

link_to

Method

What options can you use with it?

  • 2. Look at api.rubyonrails.org

(and search for link_to)

slide-52
SLIDE 52

http://api.rubyonrails.org

slide-53
SLIDE 53

link_to

Method

What options can you use with it?

  • 3. API Dock

(online Ruby and Rails docs)

http://apidock.com/rails

slide-54
SLIDE 54
slide-55
SLIDE 55

link_to

Method

What options can you use with it?

  • 4. Rails Searchable API Doc

(local download or online)

http://railsapi.com/

slide-56
SLIDE 56

http://railsapi.com/

slide-57
SLIDE 57
slide-58
SLIDE 58

<%= link_to @tweet.zombie.name, @tweet.zombie, :confirm => "Are you sure?" %>

slide-59
SLIDE 59

application.html.erb app views zombie_twitter

Views

zombies tweets index.html.erb show.html.erb

List all tweets View a tweet

layouts

The main layout

slide-60
SLIDE 60

Views

List Tweets

/app/views/tweets/index.html.erb

Tweet

tweet

class single tweet

Tweet.all

array of tweets

What they return

tweet.status tweet.zombie.name %></td> %></td>

</table> <h1>Listing tweets</h1> <table> <tr> <th>Status</th> <th>Zombie</th> </tr>

<% Tweet.all.each do |tweet| %> <tr> <td><%= <td><%= </tr> <% end %>

slide-61
SLIDE 61

Views

Create Links

/app/views/tweets/index.html.erb

tweet.status tweet.zombie.name %></td> %></td> <% Tweet.all.each do |tweet| %> <tr> <td><%= <td><%= </tr> <% end %>

slide-62
SLIDE 62

Views

/app/views/tweets/index.html.erb

link_to tweet.status tweet.zombie.name %></td> %></td> <% Tweet.all.each do |tweet| %> <tr> <td><%= <td><%= </tr> <% end %> , tweet

Create Links

slide-63
SLIDE 63

Views

/app/views/tweets/index.html.erb

link_to , tweet.zombie

Create Links

link_to tweet.status tweet.zombie.name %></td> %></td> <% Tweet.all.each do |tweet| %> <tr> <td><%= <td><%= </tr> <% end %> , tweet

slide-64
SLIDE 64

Views

Empty Table?

Tweet.all <% .each do |tweet| %> <% end %> <tr> <td><%= link_to tweet.status, tweet %></td> <td><%= link_to tweet.zombie.name, tweet.zombie %></td> </tr>

slide-65
SLIDE 65

Views

Tweet.all <% .each do |tweet| %> ... <% end %>

Empty Table?

slide-66
SLIDE 66

Views

Tweet.all <% .each do |tweet| %> <% tweets = %> tweets ... <% end %>

Empty Table?

slide-67
SLIDE 67

Views

Tweet.all <% .each do |tweet| %> <% tweets = %> tweets ... <% end %> <em>No tweets found</em> <% end %> <% if tweets.size == 0 %>

Empty Table?

slide-68
SLIDE 68

Views

Tweet.all <% .each do |tweet| %> <% tweets = %> tweets ... <% end %> <em>No tweets found</em> <% end %> <% if tweets.empty? %>

Empty Table?

slide-69
SLIDE 69

Edit & Delete Links?

<% tweets.each do |tweet| %> <tr> <td><%= link_to tweet.status, tweet %></td> <td><%= link_to tweet.zombie.name, tweet.zombie %></td> </tr> <% end %> <td><%= link_to "Edit", edit_tweet_path(tweet) %></td> <td><%= link_to "Delete", tweet, :method => :delete %></td>

slide-70
SLIDE 70

All links for Tweets

Action Code The URL Generated

List all tweets tweets_path /tweets New tweet form new_tweet_path /tweets/new

Action Code The URL Generated

Show a tweet tweet /tweets/1 Edit a tweet edit_tweet_path(tweet) /tweets/1/edit Delete a tweet tweet, :method => :delete /tweets/1

tweet = Tweet.find(1)

T h e s e p a t h s n e e d a t w e e t

<%= link_to "<link text>", <code> %>

slide-71
SLIDE 71

Zombie lab 3 Zombie lab 3

slide-72
SLIDE 72

An Introduction to Rails

w i t h G r e g g P

  • l

l a c k

Episode #4

slide-73
SLIDE 73

Chapter 4 Controllers must be eaten

brains

v

slide-74
SLIDE 74

Views Our Application Stack

4 Components Views Models Controllers

slide-75
SLIDE 75

<!DOCTYPE html> <html> <head><title>Twitter for Zombies</title></head> <body> <img src="/images/twitter.png" /> </body></html>

/app/views/tweets/show.html.erb

Show a tweet

FYI, This code sucks a little..

(we’ll fix it later)

<% tweet = Tweet.find(1) %> <h1><%= tweet.status %></h1> <p>Posted by <%= tweet.zombie.name %></p>

slide-76
SLIDE 76

/app/views/tweets/show.html.erb

<% tweet = Tweet.find(1) %> <h1><%= tweet.status %></h1> <p>Posted by <%= tweet.zombie.name %></p>

Request

/tweets/1

Controller

/app/controllers/tweets_controller.rb tweets_controller.rb app controllers zombie_twitter

slide-77
SLIDE 77

/app/views/tweets/show.html.erb

<% tweet = Tweet.find(1) %> <h1><%= tweet.status %></h1> <p>Posted by <%= tweet.zombie.name %></p>

Request

/tweets/1

Controller

/app/controllers/tweets_controller.rb app controllers zombie_twitter tweets_controller.rb tweets

tweets

tweets

slide-78
SLIDE 78

/app/views/tweets/show.html.erb

<% tweet = Tweet.find(1) %> <h1><%= tweet.status %></h1> <p>Posted by <%= tweet.zombie.name %></p>

Request

/tweets/1

/app/controllers/tweets_controller.rb

class TweetsController < ApplicationController end ...

slide-79
SLIDE 79

/app/views/tweets/show.html.erb

<% tweet = Tweet.find(1) %> <h1><%= tweet.status %></h1> <p>Posted by <%= tweet.zombie.name %></p>

Request

/tweets/1

/app/controllers/tweets_controller.rb

class TweetsController < ApplicationController end def show end

slide-80
SLIDE 80

/app/views/tweets/show.html.erb

<% tweet = Tweet.find(1) %> <h1><%= tweet.status %></h1> <p>Posted by <%= tweet.zombie.name %></p>

Request

/tweets/1

/app/controllers/tweets_controller.rb

class TweetsController < ApplicationController end def show end show

show

slide-81
SLIDE 81

/app/views/tweets/show.html.erb

<h1><%= <p>Posted by <%=

Request

/tweets/1

/app/controllers/tweets_controller.rb

class TweetsController < ApplicationController end def show end

<% %> tweet.status %></h1> tweet.zombie.name %></p>

) tweet = Tweet.find(1

slide-82
SLIDE 82

/app/views/tweets/show.html.erb

<h1><%= <p>Posted by <%=

Request

/tweets/1

/app/controllers/tweets_controller.rb

class TweetsController < ApplicationController end def show end

tweet.status %></h1> tweet.zombie.name %></p>

What about variable scope?

) tweet = Tweet.find(1

slide-83
SLIDE 83

/app/views/tweets/show.html.erb

<h1><%= <p>Posted by <%=

Request

/tweets/1

/app/controllers/tweets_controller.rb

class TweetsController < ApplicationController end def show end

tweet.status %></h1> tweet.zombie.name %></p> @ @

@

Instance Variable

@

) tweet = Tweet.find(1

slide-84
SLIDE 84

/app/views/tweets/status.html.erb

<h1><%= <p>Posted by <%=

Request

/tweets/1

/app/controllers/tweets_controller.rb

class TweetsController < ApplicationController end

tweet.status %></h1> tweet.zombie.name %></p> @ @

end def show

Render

@

) tweet = Tweet.find(1

slide-85
SLIDE 85

/app/views/tweets/status.html.erb

<h1><%= <p>Posted by <%=

Request

/tweets/1

/app/controllers/tweets_controller.rb

class TweetsController < ApplicationController end

tweet.status %></h1> tweet.zombie.name %></p> @ @

Render

end def show render :action => 'status'

@

) tweet = Tweet.find(1

slide-86
SLIDE 86

/app/views/tweets/status.html.erb

<h1><%= <p>Posted by <%=

Request

/tweets/1

/app/controllers/tweets_controller.rb

class TweetsController < ApplicationController end

tweet.status %></h1> tweet.zombie.name %></p> @ @

Render

end def show render :action => 'status'

/tweets/2 /tweets/3 /tweets/4 /tweets/5

@

) tweet = Tweet.find(1

slide-87
SLIDE 87

/app/views/tweets/status.html.erb

<h1><%= <p>Posted by <%=

Request

/tweets/1

/app/controllers/tweets_controller.rb

class TweetsController < ApplicationController end

tweet.status %></h1> tweet.zombie.name %></p> @ @ @

Render

end def show render :action => 'status'

params[:id]

/tweets/2 /tweets/3 /tweets/4 /tweets/5

) tweet = Tweet.find(1

slide-88
SLIDE 88

/app/views/tweets/status.html.erb

<h1><%= <p>Posted by <%=

Request

/tweets/1

/app/controllers/tweets_controller.rb

class TweetsController < ApplicationController end

tweet.status %></h1> tweet.zombie.name %></p> @ @ @

Render

end def show render :action => 'status'

params[:id]

/tweets/2 /tweets/3 /tweets/4 /tweets/5

) tweet = Tweet.find(params[:id]

params = { :id => "1" }

slide-89
SLIDE 89

Request

Parameters

@tweet = Tweet.create(:status => )

params[:status]

params = { :status => "I’m dead" }

@tweet = Tweet.create(:status => params[:tweet][:status])

params = {:tweet => { :status => "I’m dead" }}

We can also write as

@tweet = Tweet.create(params[:tweet])

slide-90
SLIDE 90

Request

/tweets/1

<?xml version="1.0" encoding="UTF-8"?> <tweet> <id type="integer">1</id> <status>Where can I get a good bite to eat?</status> <zombie-id type="integer">1</zombie-id> </tweet>

{"tweet":{"id":1,"status":"Where can I get a good bite to eat?","zombie_id":1}}

xml? json? xml json

slide-91
SLIDE 91

<?xml version="1.0" encoding="UTF-8"?> <tweet> <id type="integer">1</id> <status>Where can I get a good bite to eat?</status> <zombie-id type="integer">1</zombie-id> </tweet>

{"tweet":{"id":1,"status":"Where can I get a good bite to eat?","zombie_id":1}}

xml json /tweets/1. /tweets/1.

class TweetsController < ApplicationController def show @tweet = Tweet.find(params[:id]) end end

Request

/tweets/1 xml? json?

respond_to do |format| format.html # show.html.erb format.xml { render :xml => @tweet } format.json { render :json => @tweet }

slide-92
SLIDE 92

Request

/app/controllers/tweets_controller.rb

class TweetsController < ApplicationController end

Controller Actions

def show def index

View

def edit def create def update def destroy def new

List all tweets Show a single tweet Show a new tweet form Show an edit tweet form Create a new tweet Update a tweet Delete a tweet

slide-93
SLIDE 93

Adding some authorization

def edit

slide-94
SLIDE 94

Adding some authorization

def edit

/app/controllers/tweets_controller.rb

end @tweet = Tweet.find(params[:id])

app views zombie_twitter tweets edit.html.erb

slide-95
SLIDE 95

Adding some authorization

slide-96
SLIDE 96

Adding some authorization

slide-97
SLIDE 97

Adding some authorization

slide-98
SLIDE 98

Request

/app/controllers/tweets_controller.rb

class TweetsController < ApplicationController end

Redirect and Flash

def edit end if session[:zombie_id] != @tweet.zombie_id redirect_to(tweets_path end @tweet = Tweet.find(params[:id])

session

Works like a per user hash

flash[:notice]

To send messages to the user

redirect <path>

To redirect the request

flash[:notice] = "Sorry, you can’t edit this tweet" )

slide-99
SLIDE 99

Request

/app/controllers/tweets_controller.rb

class TweetsController < ApplicationController end

Redirect and Flash

def edit end if session[:zombie_id] != @tweet.zombie_id end @tweet = Tweet.find(params[:id]) redirect_to(tweets_path "Sorry, you can’t edit this tweet") , :notice =>

A l t e r n a t e S y n t a x c

  • m

b i n i n g r e d i r e c t & f l a s h

slide-100
SLIDE 100

Notice for Layouts

<!DOCTYPE html> <html> <head> <title>Twitter for Zombies</title>

<%= stylesheet_link_tag :all %> <%= javascript_include_tag :defaults %> <%= csrf_meta_tag %>

/app/views/layouts/application.html.erb

</head> <body> <img src="/images/twitter.png" />

<%= yield %>

</body></html>

slide-101
SLIDE 101

<!DOCTYPE html> <html> <head> <title>Twitter for Zombies</title>

<%= stylesheet_link_tag :all %> <%= javascript_include_tag :defaults %> <%= csrf_meta_tag %>

/app/views/layouts/application.html.erb

</head> <body> <img src="/images/twitter.png" />

<%= yield %>

</body></html> <% if flash[:notice] %> <% end %> <div id="notice"><%= flash[:notice] %></div>

Notice for Layouts

slide-102
SLIDE 102

Adding some authorization

slide-103
SLIDE 103

Adding some authorization

slide-104
SLIDE 104

Request

/app/controllers/tweets_controller.rb

class TweetsController < ApplicationController end

Controller Actions

def show def index

View

def edit def create def update def destroy def new

List all tweets Show a single tweet Show a new tweet form Show an edit tweet form Create a new tweet Update a tweet Delete a tweet

slide-105
SLIDE 105

Request

/app/controllers/tweets_controller.rb

class TweetsController < ApplicationController end

Before Filters

View

def edit def update def destroy

Show an edit tweet form Update a tweet Delete a tweet

Need Authorization

slide-106
SLIDE 106

Request

/app/controllers/tweets_controller.rb

class TweetsController < ApplicationController end def edit def update def destroy @tweet = Tweet.find(params[:id]) end @tweet = Tweet.find(params[:id]) @tweet = Tweet.find(params[:id]) end end ... ... ...

Before Filters

slide-107
SLIDE 107

Request

/app/controllers/tweets_controller.rb

class TweetsController < ApplicationController end def edit def update def destroy @tweet = Tweet.find(params[:id]) end @tweet = Tweet.find(params[:id]) @tweet = Tweet.find(params[:id]) end end ... ... ... def get_tweet end before_filter :get_tweet, :only => [:edit, :update, :destroy]

Before Filters

slide-108
SLIDE 108

Request

/app/controllers/tweets_controller.rb

class TweetsController < ApplicationController end def edit def update def destroy @tweet = Tweet.find(params[:id]) def get_tweet end before_filter :get_tweet, :only => [:edit, :update, :destroy] if session[:zombie_id] != @tweet.zombie_id flash[:notice] = "Sorry, you can’t edit this tweet" redirect_to tweets_path end def check_auth end before_filter :check_auth, :only => [:edit, :update, :destroy]

Before Filters

slide-109
SLIDE 109

Adding some authorization

slide-110
SLIDE 110

Zombie lab 4 Zombie lab 4

slide-111
SLIDE 111

An Introduction to Rails

w i t h G r e g g P

  • l

l a c k

Episode #5

slide-112
SLIDE 112

Chapter 5

Routing into darkness

slide-113
SLIDE 113

Our Application Stack

4 Components Views Models Controllers Routing

slide-114
SLIDE 114

I n

  • r

d e r t

  • p

r

  • p

e r l y fi n d t h e s e p a t h s

Action Code The URL Generated

List all tweets tweets_path /tweets New tweet form new_tweet_path /tweets/new

Action Code The URL Generated

Show a tweet tweet /tweets/1 Edit a tweet edit_tweet_path(tweet) /tweets/1/edit Delete a tweet tweet, :method => :delete /tweets/1

tweet = Tweet.find(1)

T h e s e p a t h s n e e d a t w e e t

<%= link_to "<link text>", <code> %>

slide-115
SLIDE 115

Request

/app/controllers/tweets_controller.rb

class TweetsController < ApplicationController end def show def index

View

def edit def create def update def destroy def new

List all tweets Show a single tweet Show a new tweet form Show an edit tweet form Create a new tweet Update a tweet Delete a tweet

a n d t h e s e a c t i

  • n

s

slide-116
SLIDE 116

We need to define routes

ZombieTwitter::Application.routes.draw do |map| resources :tweets end

Code The URL Generated TweetsController action

tweets_path /tweets

def index

tweet /tweet/<id>

def show

new_tweet_path /tweets/new

def new

edit_tweet_path(tweet) /tweets/<id>/edit

def edit

and a few more....

C r e a t e s w h a t w e l i k e t

  • c

a l l a “ R E S T ” f u l r e s

  • u

r c e

zombie_twitter config routes.rb

slide-117
SLIDE 117

ZombieTwitter::Application.routes.draw do |map| resources :tweets end

/config/routes.rb

Custom Routes

http://localhost:3000/new_tweet http://localhost:3000/tweets/new

class TweetsController end def new end ...

Controller name Tweets Action name new

match 'new_tweet' => "Tweets#new"

C

  • n

t r

  • l

l e r A c t i

  • n

P a t h render

slide-118
SLIDE 118
slide-119
SLIDE 119

Named Routes

match 'all' => "Tweets#index" <%= link_to "All Tweets",? %>

t w e e t s _ p a t h w

  • u

l d n ’ t w

  • r

k

http://localhost:3000/all http://localhost:3000/tweets

class TweetsController end def index end ...

Controller name Tweets Action name index

render

slide-120
SLIDE 120

match 'all' => "Tweets#index" <%= link_to "All Tweets", %>

, :as => "all_tweets"

all_tweets_path

http://localhost:3000/all http://localhost:3000/tweets

class TweetsController end def index end ...

render

Named Routes

Controller name Tweets Action name index

slide-121
SLIDE 121
slide-122
SLIDE 122

redirect to

slide-123
SLIDE 123
slide-124
SLIDE 124

Redirect

match 'all' => redirect('/tweets') match 'google' => redirect('http://www.google.com/')

http://localhost:3000/all http://localhost:3000/tweets

redirect to

slide-125
SLIDE 125

Root Route

root :to => "Tweets#index" <%= link_to "All Tweets", %> root_path

C

  • n

t r

  • l

l e r A c t i

  • n

http://localhost:3000/ http://localhost:3000/tweets

render

slide-126
SLIDE 126

/app/controllers/tweets_controller.rb

Route Parameters

/local_tweets/32828

Find all zombie tweets in this zipcode

/local_tweets/32801

if params[:zipcode] @tweets = Tweet.where(:zipcode => params[:zipcode]) else @tweets = Tweet.all end respond_to do |format| format.html # index.html.erb format.xml { render :xml => @tweets } end end def index

slide-127
SLIDE 127

referenced by params[:zipcode] in controller

match 'local_tweets/:zipcode' => 'Tweets#index' match 'local_tweets/:zipcode' => 'Tweets#index', :as => 'local_tweets'

<%= link_to "Tweets in 32828", local_tweets_path(32828) %>

Route Parameters

/local_tweets/32828

Find all zombie tweets in this zipcode

/local_tweets/32801

slide-128
SLIDE 128
slide-129
SLIDE 129
slide-130
SLIDE 130

/app/controllers/tweets_controller.rb

/github /greggpollack

match ':name' => 'Tweets#index', :as => 'zombie_tweets'

Show the tweets for these zombies

<%= link_to "Gregg", zombie_tweets_path('greggpollack') %>

/eallam /envylabs

def index if params[:name] @zombie = Zombie.where(:name => params[:name]).first @tweets = @zombie.tweets else @tweets = Tweet.all end end

Route Parameters

slide-131
SLIDE 131
slide-132
SLIDE 132

Zombie lab 5 Zombie lab 5