Sie sind auf Seite 1von 127

L!

"#L %
Deep in the CRUD
Deep in the CRUD
P&'(e)*+s,-.s:
TryRuby.org
TryRuby.org
T
w
i
t
t
e
R

f
o
r

Z
O
M
B
I
E
S
T
w
i
t
t
e
R

f
o
r

Z
O
M
B
I
E
S
D
B

T
A
B
L
E
D
B

T
A
B
L
E
{
t!"#t$
R/01
{
(w! #a$%4)
C/23m45
(w! #a$%3)
i' s()*u+ z,-.i!
R6-(i67. 89. :0.e8<b='>t 0+t?+d = @
Z/ABi6ChD2Ee4F. #1
b =
HD5G S!/0e+ 1f234y256l7%p89:s
b = { id: 3 }
S,HIl6JeK& LMEu6
MN2OiP2. Q'Rs & LMEu65
variable = { key: value } H!"# $%&i'%:
H
A
S
H
H
A
S
H
{ id: 3,
status: "I just ate some delicious brains",
zombie: "Jim" }
b =
k6S1 vD23eT
:id 3
:status "I just ate some delicious brains"
:zombie "Jim"
S()*o+"
H
A
S
H
H
A
S
H
{ id: 3,
status: "I just ate some delicious brains",
zombie: "Jim" }
HD5G S!/0e+ 1f234y256l7%p89:s
HD5G
b = { id: 3,
status: "I just ate some delicious brains",
zombie: "Jim" }
b[:status]
b[:zombie]
b[:zombie] + " said " + b[:status]
=> "Jim said I just ate some delicious brains"
=> "Jim"
=> "I just ate some delicious brains"
variable[:key] => value r!); <%=i>%:
R
E
T
R
I
E
V
E
R
E
T
R
I
E
V
E R!); (?4 $)@u!
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 ngers turned green. #FML Ash
{ id: 3,
status: "I just ate some delicious brains",
zombie: "Jim" }
R653l8
R
E
T
R
I
E
V
E
R
E
T
R
I
E
V
E
R6-(i67. 89. :0.e8<b='>t 0+t?+d = @
Z/ABi6ChD2Ee4F. #1
tU'.tT
A45Ve&
t = Tweet.find(3)
R6-(i67. 89. :0.e8<b='>t 0+t?+d = @
Z/ABi6ChD2Ee4F. #1
R
E
T
R
I
E
V
E
R
E
T
R
I
E
V
E
{ id: 3,
status: "I just ate some delicious brains",
zombie: "Jim" }
R653l8
t = Tweet.find(3)
A45Ve&
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 ngers turned green. #FML Ash
w,2E D2EoURoNOo M>c651
SABCuD): & EFGe<H6s!
T8I@e2J6m!
L,K4rL)Me2& NOPr8OT8I@e2J6m!
C
A
S
E

&
T
E
N
S
E
C
A
S
E

&
T
E
N
S
E
tU'.tT
AWX.sTYZg -[b\'1
=> 3
puts t[:id]
puts t[:status]
=> "I just ate some delicious brains."
puts t[:zombie]
=> "Jim"
R
E
T
R
I
E
V
E
R
E
T
R
I
E
V
E
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 ngers turned green. #FML Ash
tU'.tT
t = Tweet.find(3)
S
Y
N
T
A
X
S
Y
N
T
A
X
=> 3
puts t[:id]
puts t[:status]
=> "I just ate some delicious brains."
puts t[:zombie]
=> "Jim"
puts t.id
puts t.status
puts t.zombie
Should I use the hash or dot syntax? S,-.e/0 Q2%3t456:
Y7- c!8 u9% E:;<E$3y/0>x, :0 c7)?s@ABw/Co@D?r956a+ErFG?rF8&e.
A\-.r4MOe ]Rn8M^
H_]` L5aDbc
SdefAg
R6-(i67. 89. h'[p/Haoij.c8Vi89aik= l
Z/ABi6ChD2Ee4F. #2
A45Ve&
w = Weapon.find(1)
C
A
S
E

&
T
E
N
S
E
C
A
S
E

&
T
E
N
S
E
w6Mmo45
id name
1 Ash
2 Bob
3 Jim
Cr6MOe ReDn
UpkMOe De\'Oe
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
C
R
U
D
C
r
u
d
C
R
E
A
T
E
C
R
E
A
T
E
t = Tweet.new
t.status = "I <3 brains."
t.save
- QRS '%@iL91u+ -
TQ%i% T%*s2U4t2V1r2WM
RFHIpF
t = TableName.new
t.key = value
t.save
t = TableName.new(hash)
t.save
TableName.create(hash)
t = Tweet.new(
status: "I <3 brains",
zombie: "Jim")
t.save
Tweet.create(status: "I <3
brains", zombie: "Jim")
A+0?r/JCe@KLn,JM
C&'[t6 Doopq+e
R6Mr
R
e
a
d
R
e
a
d
Tweet.last
=> Returns the last tweet
Tweet.first
=> Returns the first tweet
Tweet.find(3, 4, 5)
=> Returns an array of tweets, id of 3, 4, or 5
Tweet.find(2)
=> Returns a single tweet with id of 2
Tweet.all
=> Returns all the tweets
R6X+p65at/ReDn
R
e
a
d
R
e
a
d
Tweet.where(zombie: "ash")
=> Returns all tweets from zombie named ash
Tweet.limit(10)
=> Returns the first 10 tweets
Tweet.order(:zombie)
=> Returns all tweets, ordered by zombies
Tweet.count
=> Returns total number of tweets
W! =aX =oYI0n! 6nZ 1f2[#e+%r!); Y%*h,\M (RCe(?4r2[1 8\; LR]s(/6iX[M
M6-GokChDYZi4F
M6-GokChDYZi4F
ordered by status
only the first 10
.order(:status).limit(10) .where(zombie: "ash") Tweet
Tweet.where(zombie: "ash").first
only tweets from zombie ash
=> Returns
just the first one
only tweets from ash
=> Returns
R
e
a
d
R
e
a
d
C
R
E
A
T
E
C
R
E
A
T
E
t = Tweet.find(3)
t.zombie = "EyeballChomper"
t.save
t = TableName.find(id)
t.key = value
t.save
t = TableName.find(id)
t.attributes = hash
t.save
t = Tweet.find(2)
t = TableName.update(hash)
t = Tweet.find(2)
t.attributes = {
status: "Can I munch your eyeballs?",
zombie: "EyeballChomper" }
t.save
t = Tweet.find(2)
t.update(
status: "Can I munch your eyeballs?",
zombie: "EyeballChomper")
A+0?r/JCe@KLn,JM
UPn[t6 Doopq+e
RFHIpF
D62.t6 Doopq+e
t = Tweet.find(2)
t.destroy
RFHIpF
t = Table.find(id)
t.destroy
TableName.find(id).destroy
TableName.destroy_all
Tweet.find(2).destroy
Tweet.destroy_all
A+0?r/JCe@KLn,JM
D
E
L
E
T
E
D
E
L
E
T
E
Zombie lab 1
Zombie lab 1
Models
LIFEBLOOD OF THE APP
Models
LIFEBLOOD of the app
L!"#L s
M/n.l
T&" L()#b*+,d -) t&" a./0i123i-4
H,Ky,W: ^)0l+ 6p>O0c8[0oX =oY-PnAH6t!UwA[# 8 ;a()s(R:e
M
o
d
e
l
s
M
o
d
e
l
s
V,'Vs
C/HOr/2Ee&5
R/*OeT
APtEiWMOi/HaS8M>k
M/n.lT
R
o
u
t
i
n
g
R
O
U
T
I
N
G
?
class Tweet < ActiveRecord::Base

end
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.find(3)
t_%4t+
Tweet
a>F/m,\4l+/t_%4t.r`
M
o
d
e
l
s
M
o
d
e
l
s
class Tweet < ActiveRecord::Base

end
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_%4t+
a>F/m,\4l+/t_%4t.r`
ActiveRecord::Base
M8FM (?4 LO6s+ *o2[#e2[6bD%
M
o
d
e
l
s
M
o
d
e
l
s
Tweet
class Tweet < ActiveRecord::Base

end
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_%4t+
a>F/m,\4l+/t_%4t.r`
CD)Ms
.find(3) t =
(I/"Ca/H? 7G TN%?t@#3)
M
o
d
e
l
s
M
o
d
e
l
s
Tweet
V
a
l
i
d
a
t
i
o
n
s
V
a
l
i
d
a
t
i
o
n
s
t = Tweet.new
t.save
5
VD2+dD-+o45
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_%4t+
AL3, _%d,Bt2K6n( *o2H:e8[4 8 .l8Ba bK4e(c
V
a
l
i
d
a
t
i
o
n
s
V
a
l
i
d
a
t
i
o
n
s
>> t.errors.messages
=> {status:["can't be blank"]}
>> t.errors[:status][0]
=> "can't be blank"
=> #<Tweet id: nil, status: nil, zombie: nil>
>> t = Tweet.new
>> t.save
=> false
class Tweet < ActiveRecord::Base

end
a>F/m,\4l+/t_%4t.r`
validates_presence_of :status
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
V
a
l
i
d
a
t
i
o
n
s
V
a
l
i
d
a
t
i
o
n
s
validates :status,
presence: true,
length: { minimum: 3 }
validates :status, presence: true
validates :status, length: { minimum: 3 }
presence: true
uniqueness: true
numericality: true
length: { minimum: 0, maximum: 2000 }
format: { with: /.*/ }
acceptance: true
confirmation: true
A
([:i`W*e
V
8O0d8[0oX
V
a
l
i
d
a
t
i
o
n
s
V
a
l
i
d
a
t
i
o
n
s
AOAIt456a+
O'0Io/"
A+0?r/JCe@KLn,JM
Relationships
Relationships
B6X[uT'at?'R D2VaK5at&Mue\+n t[cQ5
R
E
L
A
T
I
O
N
S
H
I
P
S
R
e
l
a
t
i
o
n
s
h
i
p
s
t_%4t+
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
W! SaX[t, Mt,/4 dRebA%M ABtQ%0r2RSn2[6bD%f
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
R
E
L
A
T
I
O
N
S
H
I
P
S
R
e
l
a
t
i
o
n
s
h
i
p
s
t_%4t+
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
t_%4t+
id name graveyard
1 Ash Glen Haven Memorial Cemetery
2 Bob Chapel Hill Cemetery
3 Jim My Fathers Basement
z,-.i!U
R
E
L
A
T
I
O
N
S
H
I
P
S
R
e
l
a
t
i
o
n
s
h
i
p
s
class Zombie <
ActiveRecord::Base
end
a>F/m,\4l+/z,-.i!.r`
has_many :tweets
PDW:aD
R
E
L
A
T
I
O
N
S
H
I
P
S
R
e
l
a
t
i
o
n
s
h
i
p
s id name graveyard
1 Ash Glen Haven Memorial Cemetery
2 Bob Chapel Hill Cemetery
3 Jim My Fathers Basement
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
z,-.i!U
t_%4t+
H_]aM_ev
a2g1m`94
T_%4t+
z,-.i!U
class Tweet < ActiveRecord::Base
end
belongs_to :zombie
S
ABCuD):
a>F/m,\4l+/t_%4t.r`
R
E
L
A
T
I
O
N
S
H
I
P
S
R
e
l
a
t
i
o
n
s
h
i
p
s
B!wxNy]aTb
a2hSe![
a2g1m`94
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
t_%4t+
id name graveyard
1 Ash Glen Haven Memorial
Cemetery
2 Bob Chapel Hill Cemetery
3 Jim My Fathers Basement
UTYZg z'Ea8Y<nT9+pT
> 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>,
#<Tweet id: 5,
status: "Your eyelids taste like bacon.", zombie_id: 1>]
> ash.tweets.count
=> 3
> ash = Zombie.find(1)
=> #<Zombie id: 1, name: "Ash", graveyard: "Glen Haven Memorial Cemetery">
> t = Tweet.create(status: "Your eyelids taste like bacon.",
zombie: ash)
=> #<Tweet id: 5, status: "Your eyelids taste like bacon.", zombie_id: 1>
R
E
L
A
T
I
O
N
S
H
I
P
S
R
e
l
a
t
i
o
n
s
h
i
p
s
z,-.i!U
R
E
L
A
T
I
O
N
S
H
I
P
S
R
e
l
a
t
i
o
n
s
h
i
p
s
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
t_%4t+
5 Your eyelids taste like bacon. 1
id name graveyard
1 Ash Glen Haven Memorial Cemetery
2 Bob Chapel Hill Cemetery
3 Jim My Fathers Basement
R
E
L
A
T
I
O
N
S
H
I
P
S
R
e
l
a
t
i
o
n
s
h
i
p
s
> t.zombie.name
=> "Ash"
> t.zombie
=> #<Zombie id: 1,
name: "Ash",
graveyard: "Glen Haven Memorial Cemetery">
> t = Tweet.find(5)
=> #<Tweet id: 5, status: "Your eyelids taste like bacon.",
zombie_id: 1>
UTYZg z'Ea8Y<nT9+pT
Zombie lab 2
Zombie lab 2
VIEW
VISUAL REPRESENTATION
VIEW
VISUAL REPRESENTATION
L!"#L {
V,'V
V
I
E
W
s
V
I
E
W
S
T&" V(56a*8e.9#s:43a;<,n
U+%: iB*e<V6c!. j?6t2K4 +%4.
o=3h:>p.?@cAB@oC
C/HOr/2Ee&5
M/n.lT
R/*OeT
APtEiWMOi/HaS8M>k
V,'Vs
V
I
E
W
s
V
I
E
W
S
Emi'rd6n Ruby
app
views
zombie_twitter
zombies
tweets
index.html.erb
Ed,qEe Ro8-+n|BokY.s
show.html.erb
LAU* 8O@ (K4e(U
VA%S 8 *w!%*
R7Ik ABMi'%Hblm
E
R
B
E
R
B
<% tweet = Tweet.find(1) %>
<h1><%= tweet.status %></h1>
<p>Posted by <%= tweet.zombie.name %></p>
S?}V DOw6'O
<!DOCTYPE html>
<html>
<head><title>Twitter for Zombies</title></head>
<body>
<header>...</header>

</body>
</html>
S
H
O
W
S
H
O
W
<% ... %> E$)@u8[4 ^W.y: <%= ... %> E$)@u8[4 ^W.y2& N/0n( ne+W@t:
/a>F/vA%Ss/t_%4t+/sQRS.h(-@.e<I
S?}V DOw6'O
/a>F/vA%Ss/t_%4t+/sQRS.h(-@.e<I
S
H
O
W
S
H
O
W
<% ... %> E$)@u8[4 ^W.y: <%= ... %> E$)@u8[4 ^W.y2& N/0n( ne+W@t:
- ^R*t!BC,\4 -
W
! 6r! :e>%6tABC (?0s29]
m7O*i>O4 $94w+.
<!DOCTYPE html>
<html>
<head><title>Twitter for Zombies</title></head>
<body>
<header>...</header>
</body>
</html>
<% tweet = Tweet.find(1) %>
<h1><%= tweet.status %></h1>
<p>Posted by </p> <%= tweet.zombie.name %>
/a>F/vA%Ss/l8o1u(U/a>F@iL)*i,B.h(-@.e<I
S?}V DOw6'O
/a>F/vA%Ss/t_%4t+/sQRS.h(-@.e<I
<%= yield %>
S
H
O
W
S
H
O
W
- b)MtZ po'%-
E$%:y2F6g! Se2H:e8[4
u+%M (?0s2[4m>O6t! .y2\4f8W@t
<!DOCTYPE html>
<html>
<head><title>Twitter for Zombies</title></head>
<body>
<header>...</header>
</body>
</html>
<% tweet = Tweet.find(1) %>
<h1><%= tweet.status %></h1>
<p>Posted by </p> <%= tweet.zombie.name %>
<%= link_to text_to_show, object_to_show %>
LABa ^%=i>%
/a>F/vA%Ss/t_%4t+/sQRS.h(-@.e<I
S
H
O
W
S
H
O
W
S?}V DOw6'O
H,Kd, Se2
m834 (?0s2)lABa qr
tweet.zombie.name, zombie_path(tweet.zombie) <%= link_to %>
<%= link_to tweet.zombie.name, tweet.zombie %>
A+0?r/JCe@KLn,JM
CP%>tF> QR6k
A+ Si(?t_%4t+,
sQR:t!/i+ .e([4r.
<% tweet = Tweet.find(1) %>
<h1><%= tweet.status %></h1>
<p>Posted by </p> <%= tweet.zombie.name %>
/a>F/vA%Ss/t_%4t+/sQRS.h(-@.e<I
S
H
O
W
S
H
O
W
S?}V DOw6'O
<%= link_to tweet.zombie.name, tweet.zombie %>
<% tweet = Tweet.find(1) %>
<h1><%= tweet.status %></h1>
<p>Posted by </p>
/a>F/vA%Ss/t_%4t+/sQRS.h(-@.e<I
S
H
O
W
S
H
O
W
S?}V DOw6'O
<%= link_to tweet.zombie.name, tweet.zombie %>
<% tweet = Tweet.find(1) %>
<h1><%= tweet.status %></h1>
<p>Posted by </p>
S,-.e/0 Q2%3t456:
WQ)* ,F*i,BM
L)] _%u+%
wA[# D9]k_t,r
L/}Ji4FauP~oW*e4-[t,}Z
W!lD siDOk, _%a<%gD); ZRP 8Uae'c
O7/n!t* (RGiL #a>F4n+ *o2I4:
1. }<k YZ 89. T}3rW'
O'%6 (5Sr@%.i,5T !8. 9%>rUV f7W dFG l48X_t7
git clone http://github.com/rails/rails.git
cd rails
C,-eaX\LAB4
L
i
n
k
L
i
n
k
OP-+o45af/ \YZk_t/
grep -rin def link_to
1. }<k YZ 89. T}3rW'
O'%6 (5Sr@%.i,5T !8. 9%>rUV f7W dFG l48X_t7
L
i
n
k
L
i
n
k
OP-+o45af/ \YZk_t/
2. }<k MO Dt+.rNqRo4[i\5.o&F
(a/A sFJTcYZoP[i/\_t7)
1. }<k YZ 89. T}3rW'
O'%6 (5Sr@%.i,5T !8. 9%>rUV f7W dFG l48X_t7
L
i
n
k
L
i
n
k
OP-+o45af/ \YZk_t/
2. }<k MO Dt+.rNqRo4[i\5.o&F
(a/A sFJTcYZoP[i/\_t7)
1. }<k YZ 89. T}3rW'
O'%6 (5Sr@%.i,5T !8. 9%>rUV f7W dFG l48X_t7
L
i
n
k
L
i
n
k
OP-+o45af/ \YZk_t/
2. }<k MO Dt+.rNqRo4[i\5.o&F
(a/A sFJTcYZoP[i/\_t7)
...
<%= link_to tweet.zombie.name,
tweet.zombie,
confirm: Are you sure? %>
/a>F/vA%Ss/t_%4t+/sQRS.h(-@.e<I
app
views
zombie_twitter
index.html.erb
application.html.erb
layouts
TQ%m89] D)ko7[
zombies
tweets
show.html.erb
LAU* 8O@ (K4e(U
VA%S 8 *w!%*
V
I
E
W
s
V
I
E
W
S
L,5O :0.e85
/a'D/v4%]s/tN%?t9/i/A?x.h,)[.eP^
tweet.status
tweet.zombie.name
%></td>
%></td>
</table>
<h1>Listing tweets</h1>
<table>
<tr>
<th>Status</th>
<th>Zombie</th>
</tr>
<% L75E ,VTo2_# FJ&h@0]eF0 %>
<tr>
<td><%=
<td><%=
</tr>
<% end %>
L,5O :0.e85
/a'D/v4%]s/tN%?t9/i/A?x.h,)[.eP^
Tweet
tweet
c+J3s
s48`lFCwF%C
Tweet.all
aPW>y@5Z ,a?e,"
WYJC ,V?y@W?t2W6
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 %>
L
i
n
k
L
i
n
k
/a>F/vA%Ss/t_%4t+/iX\4x.h(-@.e<I
C&'[t6i41
tweet.status
tweet.zombie.name
%></td>
%></td>
<% Tweet.all.each do |tweet| %>
<tr>
<td><%=
<td><%=
</tr>
<% end %>
tweet.zombie.name
link_to tweet.status
<% Tweet.all.each do |tweet| %>
<tr>
<td><%=
<td><%=
</tr>
<% end %>
, tweet
/a>F/vA%Ss/t_%4t+/iX\4x.h(-@.e<I
C&'[t6i41
L
i
n
k
L
i
n
k
link_to
%></td>
%></td> , tweet.zombie
EptOy c[b\'
Tweet.all <% .each do |tweet| %>
...
<% end %>
V
I
E
W
s
V
I
E
W
S
EptOy c[b\'
V
I
E
W
s
V
I
E
W
S
Tweet.all
tweets
<% tweets =
...
<% end %>
.each do |tweet| %>
EptOy c[b\'
Tweet.all
<%
<% tweets = %>
...
<% end %>
<em>No tweets found</em>
<% end %>
<% if tweets.size == 0 %>
V
I
E
W
s
V
I
E
W
S
tweets.each do |tweet| %>
<% 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>
EkYO & 'Ee8'aL,HJs
V
I
E
W
s
V
I
E
W
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)
TQ%Me2F6tQUn!%; 8 *w!%*
A\2aL,HJs <r cVe6-1
<%= link_to text_to_show, code %> LABa ^%=i>%:
V
I
E
W
s
V
I
E
W
S
Zombie lab 3
Zombie lab 3
Controllers
BRAINS OF THE APP
Controllers
BRAINS OF THE APP
L!"#L
C/HOr/2Ee&
C
o
n
t
r
o
l
l
e
r
s
C
O
N
T
R
O
L
L
E
R
s
T&" BD2@n$ -) t&" a./0i123i-4
TQ%bAB;iXub![Se!BtQ%M,\4l2)]d2[#e2v0e_
R/*OeT
APtEiWMOi/HaS8M>k
V,'Vs
C/HOr/2Ee&5
M/n.lT
R
o
u
t
i
n
g
R
O
U
T
I
N
G
/a>F/vA%Ss/t_%4t+/sQRS.h(-@.e<I
S?}V _Ow6'O
TQ9M LR;e2[6s(%M D9ae2/1t(%; `/6iXU. j%wAO@ w9x A[l8[4r.
<% tweet = Tweet.find(1) %>
<h1><%= tweet.status %></h1>
<p>Posted by <%= tweet.zombie.name %></p>
<!DOCTYPE html>
<html>
<head><title>Twitter for Zombies</title></head>
<body>
<header>...</header>

</body>
</html>
app
zombie_twitter
controllers
tweets_controller.rb
C
o
n
t
r
o
l
l
e
r
s
C
O
N
T
R
O
L
L
E
R
s
/a>F/vA%Ss/t_%4t+/sQRS.h(-@.e<I
<% tweet = Tweet.find(1) %>
<h1><%= tweet.status %></h1>
<p>Posted by <%= tweet.zombie.name %></p>
R63eT-
C/HOr/2Ee&5
/a>F/c,B*r,O@e<U/t_%4t+_c,B*r,O@e<.r`
/t_%4t+/1
/t_%4t+/1
/a>F/vA%Ss/t_%4t+/sQRS.h(-@.e<I
<% tweet = Tweet.find(1) %>
<h1><%= tweet.status %></h1>
<p>Posted by <%= tweet.zombie.name %></p>
app
zombie_twitter
controllers
tweets_controller.rb
C
o
n
t
r
o
l
l
e
r
s
C
O
N
T
R
O
L
L
E
R
s
R63eT-
C/HOr/2Ee&5
/a>F/c,B*r,O@e<U/t_%4t+_c,B*r,O@e<.r`
t_%4t+
t_%4t+
t_%4t+
I( 0s2B1 LR0nL9;eXH4 (?6t2[#e2K1r' t_%4t+ AU
f,W]d29] (?4 Eym, (?4 LR]t<R@l!/n8-4, 8B; (?4
vA%S wR@d!/.
...
R63eT- /t_%4t+/1
/a>F/c,B*r,O@e<U/t_%4t+_c,B*r,O@e<.r`
class TweetsController < ApplicationController
end
/a>F/vA%Ss/t_%4t+/sQRS.h(-@.e<I
<% tweet = Tweet.find(1) %>
<h1><%= tweet.status %></h1>
<p>Posted by <%= tweet.zombie.name %></p>
R
E
Q
U
E
S
T
R
E
Q
U
E
S
T
/a>F/vA%Ss/t_%4t+/sQRS.h(-@.e<I
<% tweet = Tweet.find(1) %>
<h1><%= tweet.status %></h1>
<p>Posted by <%= tweet.zombie.name %></p>
def show
end
R63eT- /t_%4t+/1
/a>F/c,B*r,O@e<U/t_%4t+_c,B*r,O@e<.r`
class TweetsController < ApplicationController
end
tweet = Tweet.find(1)
R
E
Q
U
E
S
T
R
E
Q
U
E
S
T
/a>F/vA%Ss/t_%4t+/sQRS.h(-@.e<I
<% tweet = Tweet.find(1) %>
<h1><%= tweet.status %></h1>
<p>Posted by <%= tweet.zombie.name %></p>
def show
end
R63eT- /t_%4t+/1
/a>F/c,B*r,O@e<U/t_%4t+_c,B*r,O@e<.r`
class TweetsController < ApplicationController
end
sQRS
show
R
E
Q
U
E
S
T
R
E
Q
U
E
S
T
def show
end
R63eT- /t_%4t+/1
/a>F/c,B*r,O@e<U/t_%4t+_c,B*r,O@e<.r`
class TweetsController < ApplicationController
end
/a>F/vA%Ss/t_%4t+/sQRS.h(-@.e<I
<% tweet = Tweet.find(1) %>
<h1><%= tweet.status %></h1>
<p>Posted by <%= tweet.zombie.name %></p>
tweet = Tweet.find(1)
R
E
Q
U
E
S
T
R
E
Q
U
E
S
T
TQ9M AUwQ%:e2K4 (oGiL)@lZ =aDOo7/m,\4l+,
S, @e(s2V0x2RPr2H1d!c
def show
end
R63eT- /t_%4t+/1
/a>F/c,B*r,O@e<U/t_%4t+_c,B*r,O@e<.r`
class TweetsController < ApplicationController
end
tweet = Tweet.find(1)
/a>F/vA%Ss/t_%4t+/sQRS.h(-@.e<I
<h1><%= tweet.status %></h1>
<p>Posted by <%= tweet.zombie.name %></p>
What about variable scope? S,-.e/0 Q2%3t456:
R
E
Q
U
E
S
T
R
E
Q
U
E
S
T
grants view access to variables with @ I/"Ca/H? bJTi!^[e:
def show
end
/a>F/c,B*r,O@e<U/t_%4t+_c,B*r,O@e<.r`
class TweetsController < ApplicationController
end
tweet = Tweet.find(1)
/a>F/vA%Ss/t_%4t+/sQRS.h(-@.e<I
<h1><%= tweet.status %></h1>
<p>Posted by <%= tweet.zombie.name %></p>
@
@
@
R63eT- /t_%4t+/1
R
E
Q
U
E
S
T
R
E
Q
U
E
S
T
def show


/a>F/c,B*r,O@e<U/t_%4t+_c,B*r,O@e<.r`
class TweetsController < ApplicationController
end
tweet = Tweet.find(1)
/a>F/vA%Ss/t_%4t+/s()*u+.h(-@.e<I
<h1><%= tweet.status %></h1>
<p>Posted by <%= tweet.zombie.name %></p>
@
@
@
R6Hre&YZg MaD,e&'Zt "+eU
render action: 'status'
end
R
E
Q
U
E
S
T
R
E
Q
U
E
S
T
tweet = Tweet.find( )
P!W>m9ceUREe:
def show


/a>F/c,B*r,O@e<U/t_%4t+_c,B*r,O@e<.r`
class TweetsController < ApplicationController
end
@
AWX.p8YZg [rDA.t61
render action: 'status'
end
/t_%4t+/1
/t_%4t+/2
/t_%4t+/3
/t_%4t+/4
/t_%4t+/5
params[:id]
params = { id: "1" }
/t_%4t+/6
R
E
Q
U
E
S
T
R
E
Q
U
E
S
T
@tweet = Tweet.create(status: params[:status])
P
A
R
A
M
S
P
A
R
A
M
S
PD[m6-.rT
params = { status: "Im dead" }
/t_%4t+rMt8[Ps=Im2\4a'
@tweet = Tweet.create(status: params[:tweet][:status])
params = { tweet: {status: "Im dead" }}
@tweet = Tweet.create(params[:tweet])
AD[4rX)*e2zkn()x
/t_%4t+r*w!%*[s()*u+]=Im2\4a'
@tweet = Tweet.create(params[:tweet])
require(:tweet)
permit(:status)
P
A
R
A
M
S
P
A
R
A
M
S
PD[m6-.rT
I| |a||: 1 wele|tqu|le1 lou:e
WF6eFA t73pFHIf(ChFEaPJde,%T e%L N% rFfSiP%
A/A tY% a,0Tig-Ce9]e@aIl+EeP)It@0B g% sF0
S8<n|a&Me8'(s
params = { tweet: {status: "Im dead" }}
/t_%4t+r*w!%*[s()*u+]=Im2\4a'
- ^R*t!BC,\4 -
TQ9M LRPl' .e2\6nT%:o7Uf EU4r+ eiT?*
b! 6bD%t, Me( 6nZ 6t(/0b7[4s{
@tweet = Tweet.create(params
S!|cn| |a!|sRtqu|le1 cn|]w|-|:
C$hiT:jk o! UlmiT:jk
w
|!| M
nopIloq A,0Tig-Ce9
@tweet = Tweet.create(params[:tweet])
P
A
R
A
M
S
P
A
R
A
M
S
require(:tweet) permit(:status) . . )
IrChFW? N%Te@)Sl,RElFCh48`s@a? /%?dFA t7EeP)It, N% c7-[d@-3e@J6 !WTa(
params.require(:tweet).permit([:status, :location])
S8<n|a&Me8'(s
/t_%4t+r*w!%*[s()*u+]=Im2\4a'
params = { tweet: {status: "Im dead" }}
P
A
R
S
E
P
A
R
S
E
<?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}}
X|}
J~
R65mo4naw,-G g /aJ?
/t_%4t+/1
P
A
R
S
E
P
A
R
S
E
{"tweet":{"id":1,
"status":"Where can I get a good bite to eat?",
"zombie_id":1}}
J~
R65mo4naw,-G cL }( ]xN
/t_%4t+/1
tweet = Tweet.find( )
def show


/a>F/c,B*r,O@e<U/t_%4t+_c,B*r,O@e<.r`
class TweetsController < ApplicationController
end
@
end
params[:id]
respond_to do |format|
format.html # show.html.erb
format.json { render json: @tweet }
end
.j$+E
P
A
R
S
E
P
A
R
S
E
X|}
R65mo4naw,-G cL, ]xN, g
/t_%4t+/1
tweet = Tweet.find( )
def show


/a>F/c,B*r,O@e<U/t_%4t+_c,B*r,O@e<.r`
class TweetsController < ApplicationController
end
@
end
params[:id]
respond_to do |format|
format.html # show.html.erb
format.json { render json: @tweet }
end
.xF?
format.xml { render xml: @tweet }
<?xml version="1.0" encoding="UTF-8"?> ...
/a>F/c,B*r,O@e<U/t_%4t+_c,B*r,O@e<.r`
C/HOr/2Ee&c8Y<nT
class TweetsController < ApplicationController
end
V,'Vs
R
E
Q
U
E
S
T
R
E
Q
U
E
S
T
def show
def index
def edit
def create
def update
def destroy
def new
LAU* 8O@ (K4e(U
SQRS 8 MiXu@e2[Se![
SQRS 8 ]e_ *w!%* wR:m
SQRS 8Be'9* (K4e( o<-
C<%6t! 6 X%S (K4e(
U>\6t! 6 (K4e(
D!O4t! 6 (K4e(
C/HOr/2Ee&c8Y<nT
/a>F/c,B*r,O@e<U/t_%4t+_c,B*r,O@e<.r`
class TweetsController < ApplicationController
end
def show
def index
def edit
def create
def update
def destroy
def new
LAU* 8O@ (K4e(U
SQRS 8 MiXu@e2[Se![
SQRS 8 ]e_ *w!%* wR:m
SQRS 8Be'9* (K4e( o<-
C<%6t! 6 X%S (K4e(
U>\6t! 6 (K4e(
D!O4t! 6 (K4e(
E
D
I
T
E
D
I
T
def edit
/a>F/c,B*r,O@e<U/t_%4t+_c,B*r,O@e<.r`
end
@tweet = Tweet.find(params[:id])
T?'aEkYO _XOi/H E
D
I
T
E
D
I
T
app
views
zombie_twitter
tweets
edit.html.erb
Akn+n|op'aAN-Go&Yoa8Y<n
E
D
I
T
E
D
I
T
D
E
L
E
T
E
D
E
L
E
T
E
Akn+n|op'aAN-Go&Yoa8Y<n
D
E
L
E
T
E
D
E
L
E
T
E
Akn+n|op'aAN-Go&Yoa8Y<n
) redirect_to(tweets_path
R6n+r6XO DHr 2[s?
/a>F/c,B*r,O@e<U/t_%4t+_c,B*r,O@e<.r`
class TweetsController < ApplicationController
end
def edit
end
if session[:zombie_id] != @tweet.zombie_id
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 cant edit this tweet"
R
E
Q
U
E
S
T
R
E
Q
U
E
S
T
AD[4rX)*e2y4cAF4:
) redirect_to(tweets_path
R6n+r6XO DHr 2[s?
/a>F/c,B*r,O@e<U/t_%4t+_c,B*r,O@e<.r`
class TweetsController < ApplicationController
end
def edit
end
if session[:zombie_id] != @tweet.zombie_id
end
@tweet = Tweet.find(params[:id])
flash[:notice] ="Sorry, you cant edit this tweet"
R
E
Q
U
E
S
T
R
E
Q
U
E
S
T
redirect_to(tweets_path,
"Sorry, you cant edit this tweet") :notice =>
FD)Mh2+ ^%;i<%=t
/a>F/vA%Ss/l8o1u(U/a>F@iL)*i,B.h(-@.e<I
N/-+c6o&aK}3tT
<!DOCTYPE html>
<head>
<title>Twitter for Zombies</title>
</head>
<body>
<img src="/images/twitter.png" />
<%= yield %>
</body>
</html>
<!DOCTYPE html>
<head>
<title>Twitter for Zombies</title>
/a>F/vA%Ss/l8o1u(U/a>F@iL)*i,B.h(-@.e<I
</head>
<body>
<img src="/images/twitter.png" />
<%= yield %>
</body>
</html>
N/-+c6o&aK}3tT
<% if flash[:notice] %>
<% end %>
<div id="notice"><%= flash[:notice] %></div>
D
E
L
E
T
E
D
E
L
E
T
E
Akn+n|op'aAN-Go&Yoa8Y<n
Akn+n|op'aAN-Go&Yoa8Y<n
D
E
L
E
T
E
D
E
L
E
T
E
/a.//c-43r-?0eD5/t!"#t$_c-43r-?0eD.rG
C/HOr/2Ee&c8Y<nT
class TweetsController < ApplicationController
end
V,'Vs
R
E
Q
U
E
S
T
R
E
Q
U
E
S
T
def show
def index
def edit
def create
def update
def destroy
def new
L(53 A?0 ;H#e;5
S&+I AJiCK0eLBIe:B
S&+I AEe!3w:"3 =+Mm
S&+I A4 e%<3 ;H#e;NoDO
CD">t:> C"I ;H#e;
U.P>t:> ;H#e;
D:?#t:> ;H#e;
/a>F/c,B*r,O@e<U/t_%4t+_c,B*r,O@e<.r`
C/HOr/2Ee&c8Y<nT
class TweetsController < ApplicationController
end
V,'Vs
R
E
Q
U
E
S
T
R
E
Q
U
E
S
T
def show
def index
def edit
def create
def update
def destroy
def new
L(53 A?0 ;H#e;5
S&+I AJiCK0eLBIe:B
S&+I AEe!3w:"3 =+Mm
S&+I A4 e%<3 ;H#e;NoDO
CD">t:> C"I ;H#e;
U.P>t:> ;H#e;
D:?#t:> ;H#e;
N!%; W*h,/0z8[0oX
@tweet = Tweet.find(params[:id])
B6<r6c8Y<nT
/a>F/c,B*r,O@e<U/t_%4t+_c,B*r,O@e<.r`
class TweetsController < ApplicationController
end
R
E
Q
U
E
S
T
R
E
Q
U
E
S
T
def edit
def update
def destroy
@tweet = Tweet.find(params[:id])
end
@tweet = Tweet.find(params[:id])
end
end
...
...
...
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_action :get_tweet , only: [:edit, :update, :destroy]
/a>F/c,B*r,O@e<U/t_%4t+_c,B*r,O@e<.r`
B
E
F
O
R
E

A
C
T
I
O
N
s
B
E
F
O
R
E

A
C
T
I
O
N
s
/a>F/c,B*r,O@e<U/t_%4t+_c,B*r,O@e<.r`
class TweetsController < ApplicationController
end
def edit
def update
def destroy
@tweet = Tweet.find(params[:id])
def get_tweet
end
before_action :get_tweet , :only => [:edit, :update, :destroy]
if session[:zombie_id] != @tweet.zombie_id
flash[:notice] = "Sorry, you cant edit this tweet"
redirect_to tweets_path
end
def check_auth
end
before_action :check_auth , :only => [:edit, :update, :destroy]
B
E
F
O
R
E

A
C
T
I
O
N
s
B
E
F
O
R
E

A
C
T
I
O
N
s
Akn+n|op'aAN-Go&Yoa8Y<n
D
E
L
E
T
E
D
E
L
E
T
E
Zombie lab 4
Zombie lab 4
Routing
THROUGH RAILS
Routing
THROUGH RAILS
L!"#L
APtEiWMOi/HaS8M>k
V,'Vs
C/HOr/2Ee&5
M/n.lT
R
o
u
t
i
n
g
R
O
U
T
I
N
G
R/*OeT
IC,r%"M ;+ pD+QeD?R =<EdLBSe$" pABSs...
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)
TQ%Me2F6tQUn!%; 8 *w!%*
<%= link_to "<link text>", <code> %>
R
o
u
t
i
n
g
R
O
U
T
I
N
G
aCP t&"JeL2Tt(+Es...
/a>F/c,B*r,O@e<U/t_%4t+_c,B*r,O@e<.r`
V,'Vs
R
E
Q
U
E
S
T
R
E
Q
U
E
S
T
class TweetsController < ApplicationController
end
def show
def index
def edit
def create
def update
def destroy
def new
LAU* 8O@ (K4e(U
SQRS 8 MiXu@e2[Se![
SQRS 8 ]e_ *w!%* wR:m
SQRS 8Be'9* (K4e( o<-
C<%6t! 6 X%S (K4e(
U>\6t! 6 (K4e(
D!O4t! 6 (K4e(
W:Ee:P t-Ue=<EeLV,u;"J
ZombieTwitter::Application.routes.draw do
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....
routes.rb
C<%6tABC _?6t2K4 D9ae2[1 L)@l2)RzFE}r!U1u<H4
config
zombie_twitter
ZombieTwitter::Application.routes.draw do
resources :tweets
end
/c,BiT/r,W*e+.r`
CN5OopoN-.s
h([G://l,H6lQRMt:3/n!K_t_%4t h([G://l,H6lQRMt:3/t_%4t+/n!K
class TweetsController
end
def new
end
...
get '/new_tweet' => 'tweets#new'
C,B*r,O@e<
AL[0oX
P8[#
r!B;e<
Controller name Tweets
Action name new
R
o
u
t
i
n
g
R
O
U
T
I
N
G
R
o
u
t
i
n
g
R
O
U
T
I
N
G
Controller name Tweets
Action name index
/c,BiT/r,W*e+.r`
NDA.d <u8'1
h([G://l,H6lQRMt:3/aDO
class TweetsController
end
def index
end
...
get '/all' => 'tweets#index'
<%= link_to "All Tweets",
?
%> t_%4t+_p8[# _RPl'Bt2K1r
R
o
u
t
i
n
g
R
O
U
T
I
N
G
h([G://l,H6lQRMt:3/t_%4t+ r!B;e<
Controller name Tweets
Action name index
/c,BiT/r,W*e+.r`
NDA.d <u8'1
h([G://l,H6lQRMt:3/aDO h([G://l,H6lQRMt:3/t_%4t+
class TweetsController
end
def index
end
...
get '/all' => 'tweets#index'
<%= link_to "All Tweets", %>
, as: 'all_tweets'
all_tweets_path
r!B;e<
R
o
u
t
i
n
g
R
O
U
T
I
N
G
R
o
u
t
i
n
g
R
O
U
T
I
N
G
r6n+r6XOs -<
R
o
u
t
i
n
g
R
O
U
T
I
N
G
bN-an/0aoNad6+n,-+v6R+s /tU'.tT
W?MO WMZ U'ad/
W?MO ,aoNatU'.tT3s6nat/Be <u4naa8/a\2,
R
o
u
t
i
n
g
R
O
U
T
I
N
G
get '/all' => redirect('/tweets')
get '/google' => redirect('http://www.google.com/')
R6n+r6XO
h([G://l,H6lQRMt:3/aDO h([G://l,H6lQRMt:3/t_%4t+
r!\0r!H*_t,
R
o
u
t
i
n
g
R
O
U
T
I
N
G
root to: "tweets#index"
<%= link_to "All Tweets", %> root_path
C,B*r,O@e<
AL[0oX
R/}O z}3t6
h([G://l,H6lQRMt:3/ h([G://l,H6lQRMt:3/t_%4t+ r!B;e<
R
o
u
t
i
n
g
R
O
U
T
I
N
G
/l,H6l_t_%4t+/38
/a>F/c,B*r,O@e<U/t_%4t+_c,B*r,O@e<.r`
R/*Oe [rDA.t61
/l,H6l_t_%4t+/31
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
F
AB
;

8
O@
d
R
e
b
A%

t
_
%
4
t
+

iX

*
h
AU

z
AF

c
,
\
4
r!V4r!B=e' .y p8/6m+[:zAF=o'%] iX =oX[:oDO4r
get '/local_tweets/:zipcode' => 'tweets#index'
get '/local_tweets/:zipcode'
=> 'tweets#index', as: 'local_tweets'
<%= link_to "Tweets in 32828", local_tweets_path(32828) %>
/l,H6l_t_%4t+/38
/l,H6l_t_%4t+/31
F
AB
;

8
O@
d
R
e
b
A%

t
_
%
4
t
+

iX

*
h
AU

z
AF

c
,
\
4
R/*Oe [rDA.t61
R
o
u
t
i
n
g
R
O
U
T
I
N
G
R
o
u
t
i
n
g
R
O
U
T
I
N
G
/a>F/c,B*r,O@e<U/t_%4t+_c,B*r,O@e<.r`
/gA[#u`
/g<%Cg>R@l8Ha
get ':name' => 'tweets#index', as: 'zombie_tweets'
SQRS
(?4 (K
4e(U
f,/tQ%Me21m
`94s
<%= link_to "Gregg", zombie_tweets_path('greggpollack') %>
/e8O@aY
/eX5kl8IM
def index
if params[:name]
@zombie = Zombie.where(name: params[:name]).first
@tweets = @zombie.tweets
else
@tweets = Tweet.all
end
end
R/*Oe [rDA.t61
R
o
u
t
i
n
g
R
O
U
T
I
N
G
Zombie lab 5
Zombie lab 5

Das könnte Ihnen auch gefallen