Relationships RELATIONSHIPS z omb i es id name graveyard Ash Glen Haven Memorial Cemetery 1 2 Bob Chapel Hill Cemetery a Zo m bie H AS M ANY 3 Jim My Father’s Basement T wee t s a pp /m ode l s /z omb i e .r b t wee t s id status zombie_id class Zombie < 1 1 Where can I get a good bite to eat? ActiveRecord::Base My left arm is missing, but I don't 2 2 has_many :tweets care. 3 I just ate some delicious brains. 3 P lur a l end 4 OMG, my fingers turned green. #FML 1
Relationships RELATIONSHIPS t wee t s id status zombie_id 1 1 Where can I get a good bite to eat? My left arm is missing, but I don't 2 2 care. 3 I just ate some delicious brains. 3 a Tw e et B ELO N GS T O 4 OMG, my fingers turned green. #FML 1 a Zo m bie z omb i es a pp /m ode l s /t wee t.r b id name graveyard Ash Glen Haven Memorial 1 class Tweet < ActiveRecord::Base Cemetery belongs_to :zombie 2 Bob Chapel Hill Cemetery 3 Jim My Father’s Basement S ing u lar end
Relationships RELATIONSHIPS U sin g Rel a tio n shi p s > 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> > ash.tweets.count => 3 > 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>]
Relationships RELATIONSHIPS t wee t s id status zombie_id 1 1 Where can I get a good bite to eat? My left arm is missing, but I don't 2 2 care. 3 I just ate some delicious brains. 3 4 OMG, my fingers turned green. #FML 1 5 Your eyelids taste like bacon. 1 z omb i es id name graveyard Ash Glen Haven Memorial Cemetery 1 2 Bob Chapel Hill Cemetery 3 Jim My Father’s Basement
Relationships RELATIONSHIPS U sin g Rel a tio n shi p s > t = Tweet.find(5) => #<Tweet id: 5, status: "Your eyelids taste like bacon.", zombie_id: 1> > t.zombie => #<Zombie id: 1, name: "Ash", graveyard: "Glen Haven Memorial Cemetery"> > t.zombie.name => "Ash"
Zombie lab 2 Zombie lab 2
VIEW VIEW VISUAL REPRESENTATION VISUAL REPRESENTATION L EVE L 3
VIEWS VIEWs V iew U ser Int e rfa c e . Wha t we see . T he V isu a l R e pre s ent a tio n o f t h e a p pli c ati o n
VIEWS VIEWs A ppl i cat i on S tac k R out e s V iew s C ont r oll e rs M ode l s
ERB ERB zombie_twitter Ed ibl e Ro tti n g Bo die s app views zombies tweets L ist all twe e ts index.html.erb R uby ins i de H TML show.html.erb Em bed d ed Ruby V iew a t w eet
SHOW SHOW S how a t w eet /a pp /v iew s/t wee t s /s how .h tml .e rb <!DOCTYPE html> <html> <head><title>Twitter for Zombies</title></head> <body> <header>...</header> <% tweet = Tweet.find(1) %> <h1><%= tweet.status %></h1> <p>Posted by <%= tweet.zombie.name %></p> </body> </html> E val u ate Rub y: E val u ate Rub y & Pri n t R e sul t: <% ... %> <%= ... %>
SHOW SHOW S how a t w eet /a pp /v iew s/t wee t s /s how .h tml .e rb <!DOCTYPE html> - e d o C n e <html> t t o R - n i s i h t g n i t a e p e <head><title>Twitter for Zombies</title></head> r e r a e W . s w e i v e l p i t <body> l u m <header>...</header> <% tweet = Tweet.find(1) %> <h1><%= tweet.status %></h1> <%= tweet.zombie.name %> </p> <p>Posted by </body> </html> E val u ate Rub y: E val u ate Rub y & Pri n t R e sul t: <% ... %> <%= ... %>
SHOW SHOW S how a t w eet /a pp /v iew s/l ayo u ts /a ppl i cat i on .h tml .e rb <!DOCTYPE html> <html> <head><title>Twitter for Zombies</title></head> <body> <header>...</header> - Tas t y C o de - <%= yield %> E ver y pa g e w e cr e ate </body> u ses thi s te m pla t e b y de f aul t </html> /a pp /v iew s/t wee t s /s how .h tml .e rb <% tweet = Tweet.find(1) %> <h1><%= tweet.status %></h1> <%= tweet.zombie.name %> </p> <p>Posted by
SHOW SHOW S how a t w eet e w o d w o H /a pp /v iew s/t wee t s /s how .h tml .e rb ? ? k n i l a s h i t e k a m <% tweet = Tweet.find(1) %> <h1><%= tweet.status %></h1> <%= tweet.zombie.name %> </p> <p>Posted by C rea t e a Lin k <%= link_to tweet.zombie.name, zombie_path(tweet.zombie) %> A s w i th t wee t s , A lte r nat e Sy n tax s hor t er i s b e tte r. <%= link_to tweet.zombie.name, tweet.zombie %> L ink Rec i pe <%= link_to text_to_show, object_to_show %>
SHOW SHOW S how a t w eet /a pp /v iew s/t wee t s /s how .h tml .e rb <% tweet = Tweet.find(1) %> <h1><%= tweet.status %></h1> </p> <p>Posted by <%= link_to tweet.zombie.name, tweet.zombie %>
SHOW SHOW S how a t w eet /a pp /v iew s/t wee t s /s how .h tml .e rb <% tweet = Tweet.find(1) %> <h1><%= tweet.status %></h1> </p> <p>Posted by <%= link_to tweet.zombie.name, tweet.zombie %> S tud e nt Q ues t ion : W hat opt i ons can we u se w ith lin k_t o? W e ’l l B i lly , we a re g lad you ask e d! O ur n ext top i c h a ppe n s t o be : L ook i ng u p D o cum e nta t ion
Link Link O pti o ns f or lin k_t o 1. Loo k in the sou r ce O pen you r ed i tor and sea r ch f or “d ef l ink _t o ” C omm a nd L ine git clone http://github.com/rails/rails.git cd rails grep -rin “def link_to”
Link Link O pti o ns f or lin k_t o 1. Loo k in the sou r ce O pen you r ed i tor and sea r ch f or “d ef l ink _t o ” 2. Loo k at api .r uby o nra i ls .o rg (a nd s ear c h f o r l i nk _t o )
Link Link O pti o ns f or lin k_t o 1. Loo k in the sou r ce O pen you r ed i tor and sea r ch f or “d ef l ink _t o ” 2. Loo k at api .r uby o nra i ls .o rg (a nd s ear c h f o r l i nk _t o )
Link Link O pti o ns f or lin k_t o 1. Loo k in the sou r ce O pen you r ed i tor and sea r ch f or “d ef l ink _t o ” 2. Loo k at api .r uby o nra i ls .o rg (a nd s ear c h f o r l i nk _t o ) /a pp /v iew s/t wee t s /s how .h tml .e rb ... <%= link_to tweet.zombie.name, tweet.zombie, confirm: “Are you sure?” %>
VIEWS VIEWs zombie_twitter app views T he m ain lay o ut layouts application.html.erb zombies L ist all twe e ts tweets index.html.erb show.html.erb V iew a t w eet
L ist Twe e ts /a pp /v iew s/t wee t s /i nde x.h tml .e rb <h1>Listing tweets</h1> <table> <tr> <th>Status</th> <th>Zombie</th> </tr> <% L oop thr o ugh eac h tw e et %> <tr> tweet.status <td><%= %></td> tweet.zombie.name %></td> <td><%= </tr> <% end %> </table>
W hat the y re t urn L ist Twe e ts c las s Tweet /a pp /v iew s/t wee t s /i nde x.h tml .e rb a rra y of twe e ts Tweet.all <h1>Listing tweets</h1> s ing l e t w eet tweet <table> <tr> <th>Status</th> <th>Zombie</th> </tr> <% Tweet.all.each do |tweet| %> <tr> tweet.status <td><%= %></td> tweet.zombie.name %></td> <td><%= </tr> <% end %> </table>
Link Link C rea t e L i nks /a pp /v iew s/t wee t s /i nde x.h tml .e rb <% Tweet.all.each do |tweet| %> <tr> tweet.status <td><%= %></td> tweet.zombie.name %></td> <td><%= </tr> <% end %>
Link Link C rea t e L i nks /a pp /v iew s/t wee t s /i nde x.h tml .e rb <% Tweet.all.each do |tweet| %> <tr> link_to tweet.status , tweet %></td> <td><%= link_to tweet.zombie.name <td><%= %></td> , tweet.zombie </tr> <% end %>
VIEWS VIEWs E mpt y Ta b le? <% Tweet.all .each do |tweet| %> ... <% end %>
VIEWS VIEWs E mpt y Ta b le? <% tweets = Tweet.all tweets .each do |tweet| %> ... <% end %>
VIEWS VIEWs E mpt y Ta b le? <% tweets = Tweet.all %> <% tweets.each do |tweet| %> ... <% end %> <% if tweets.size == 0 %> <em>No tweets found</em> <% end %>
VIEWS VIEWs E dit & Del e te L ink s <% tweets.each do |tweet| %> <tr> <td><%= link_to tweet.status, tweet %></td> <td><%= link_to tweet.zombie.name, tweet.zombie %></td> <td><%= link_to "Edit", edit_tweet_path(tweet) %></td> <td><%= link_to "Delete", tweet, :method => :delete %></td> </tr> <% end %>
VIEWS VIEWs A ll L ink s Fo r Tw e ets The URL Action Code Generated List all tweets tweets_path /tweets New tweet form new_tweet_path /tweets/new T hes e pa t hs n eed a t w eet tweet = Tweet.find(1) Action Code The URL Show a tweet tweet /tweets/1 Edit a tweet edit_tweet_path(tweet) /tweets/1/edit Delete a tweet tweet, :method => :delete /tweets/1 L ink Rec i pe : <%= link_to text_to_show, code %>
Zombie lab 3 Zombie lab 3
Controllers Controllers BRAINS OF THE APP BRAINS OF THE APP L EVE L 4
CONTROLLERs Controllers C ont r oll e r T he b ind i ng b etw e en t he M ode l an d th e Vi e w T he B rai n s of t he a ppl i cat i on
ROUTING Routing A ppl i cat i on S tac k V iew s M ode l s C ont r oll e rs R out e s
S how A t w eet /a pp /v iew s/t wee t s /s how .h tml .e rb <!DOCTYPE html> <html> <head><title>Twitter for Zombies</title></head> <body> <header>...</header> <% tweet = Tweet.find(1) %> <h1><%= tweet.status %></h1> <p>Posted by <%= tweet.zombie.name %></p> </body> </html> T his cod e ta s tes lik e ro t ted bra i ns . We w ill fix it l ate r.
CONTROLLERs Controllers R equ e st /t wee t s /1 zombie_twitter /a pp /c ont r oll e rs /t wee t s _c ont r oll e r .r b app C ont r oll e rs controllers tweets_controller.rb /a pp /v iew s/t wee t s /s how .h tml .e rb <% tweet = Tweet.find(1) %> <h1><%= tweet.status %></h1> <p>Posted by <%= tweet.zombie.name %></p>
CONTROLLERs Controllers R equ e st /t wee t s /1 t wee t s zombie_twitter /a pp /c ont r oll e rs /t wee t s _c ont r oll e r .r b t wee t s app C ont r oll e rs controllers tweets_controller.rb /a pp /v iew s/t wee t s /s how .h tml .e rb t wee t s <% tweet = Tweet.find(1) %> <h1><%= tweet.status %></h1> <p>Posted by <%= tweet.zombie.name %></p> I t i s no coi n cid e nce tha t th e wo r d ‘t wee t s ’ is f oun d in the URL , the con t rol l er n ame , and the v iew fol d er .
REQUEST REQUEST R equ e st /t wee t s /1 /a pp /c ont r oll e rs /t wee t s _c ont r oll e r .r b class TweetsController < ApplicationController ... end /a pp /v iew s/t wee t s /s how .h tml .e rb <% tweet = Tweet.find(1) %> <h1><%= tweet.status %></h1> <p>Posted by <%= tweet.zombie.name %></p>
REQUEST REQUEST R equ e st /t wee t s /1 /a pp /c ont r oll e rs /t wee t s _c ont r oll e r .r b class TweetsController < ApplicationController def show end end /a pp /v iew s/t wee t s /s how .h tml .e rb <% tweet = Tweet.find(1) %> tweet = Tweet.find(1) <h1><%= tweet.status %></h1> <p>Posted by <%= tweet.zombie.name %></p>
REQUEST REQUEST R equ e st /t wee t s /1 /a pp /c ont r oll e rs /t wee t s _c ont r oll e r .r b class TweetsController < ApplicationController def show show end end /a pp /v iew s/t wee t s /s how .h tml .e rb s how <% tweet = Tweet.find(1) %> <h1><%= tweet.status %></h1> <p>Posted by <%= tweet.zombie.name %></p>
REQUEST REQUEST R equ e st /t wee t s /1 /a pp /c ont r oll e rs /t wee t s _c ont r oll e r .r b class TweetsController < ApplicationController def show T his is w her e we typ i cal l y c a ll o ur m ode l s , end S o l e t ’s fi x ou r co d e! end /a pp /v iew s/t wee t s /s how .h tml .e rb <% tweet = Tweet.find(1) %> tweet = Tweet.find(1) <h1><%= tweet.status %></h1> <p>Posted by <%= tweet.zombie.name %></p>
REQUEST REQUEST R equ e st /t wee t s /1 /a pp /c ont r oll e rs /t wee t s _c ont r oll e r .r b class TweetsController < ApplicationController def show tweet = Tweet.find(1) end end /a pp /v iew s/t wee t s /s how .h tml .e rb <h1><%= tweet.status %></h1> <p>Posted by <%= tweet.zombie.name %></p> S tud e nt Q ues t ion : What about variable scope?
REQUEST REQUEST R equ e st /t wee t s /1 /a pp /c ont r oll e rs /t wee t s _c ont r oll e r .r b class TweetsController < ApplicationController def show @ tweet = Tweet.find(1) end end /a pp /v iew s/t wee t s /s how .h tml .e rb <h1><%= tweet.status %></h1> @ @ <p>Posted by <%= tweet.zombie.name %></p> I nst a nce Var i abl e: grants view access to variables with @
REQUEST REQUEST R end e rin g a D iff e ren t Vi e w /a pp /c ont r oll e rs /t wee t s _c ont r oll e r .r b class TweetsController < ApplicationController def show @ tweet = Tweet.find(1) render action: 'status' end end /a pp /v iew s/t wee t s /s tat u s .h tml .e rb <h1><%= tweet.status %></h1> @ @ <p>Posted by <%= tweet.zombie.name %></p>
REQUEST REQUEST /t wee t s /1 /t wee t s /4 A cce p tin g Pa r ame t ers /t wee t s /2 /t wee t s /5 /t wee t s /6 /t wee t s /3 /a pp /c ont r oll e rs /t wee t s _c ont r oll e r .r b class TweetsController < ApplicationController def show params[:id] @ tweet = Tweet.find( ) render action: 'status' end end P ara m s R e cip e: params = { id: "1" }
PARAMS PARAMS P ara m ete r s /t wee t s?s t atu s=I’m de a d params = { status: "I’m dead" } @tweet = Tweet.create(status: params[:status] ) /t wee t s?t w eet [s tat u s ]=I’m de a d params = { tweet: {status: "I’m dead" }} @tweet = Tweet.create(status: params[:tweet][:status] ) A lte r nat e Sy n tax @tweet = Tweet.create( params[:tweet] )
PARAMS PARAMS P ara m ete r s /t wee t s?t w eet [s tat u s ]=I’m de a d params = { tweet: {status: "I’m dead" }} - e d o C n e t t o R - t h g i m s r e s @tweet = Tweet.create( params[:tweet] ) U ! s u o r e g n a d e b d u l o c s h i ! T s e t u b r i t t a y n a t e s o t e b l a e b I n R a ils 4 w e ar e re q uir e d t o us e S tro n g P a ram e ter s W e n e ed t o s p eci f y t h e p a ram e ter key we r equ i re require(:tweet) A nd t he a ttr i but e s w e wi l l p e rmi t to be s et permit(:status)
PARAMS PARAMS S tro n g P a ram e ter s /t wee t s?t w eet [s tat u s ]=I’m de a d params = { tweet: {status: "I’m dead" }} @tweet = Tweet.create( params . require(:tweet) permit(:status) ) . I f t h ere wer e mu l tip l e t h ing s we nee d ed t o p e rmi t, we c oul d us e an arr a y params. require(:tweet).permit([:status, :location]) @tweet = Tweet.create( params[:tweet] ) n : e h w y l n o d e r i u q e R s m a r a P g n o r t S G N I T A D P U r o G N I T A E R C s e t u b i r t t A E L P I T L U M h t i w
PARSE PARSE R esp o nd w ith XML or J SON ? /t wee t s /1 X ML <?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> J SON {"tweet":{"id":1, "status":"Where can I get a good bite to eat?", "zombie_id":1}}
PARSE PARSE R esp o nd w ith HTM L or JSO N .j son /t wee t s /1 /a pp /c ont r oll e rs /t wee t s _c ont r oll e r .r b class TweetsController < ApplicationController def show params[:id] @ tweet = Tweet.find( ) respond_to do |format| format.html # show.html.erb format.json { render json: @tweet } end end end J SON {"tweet":{"id":1, "status":"Where can I get a good bite to eat?", "zombie_id":1}}
PARSE PARSE R esp o nd w ith HTM L, JSO N, XML .x ml /t wee t s /1 /a pp /c ont r oll e rs /t wee t s _c ont r oll e r .r b class TweetsController < ApplicationController def show params[:id] @ tweet = Tweet.find( ) respond_to do |format| format.html # show.html.erb format.json { render json: @tweet } format.xml { render xml: @tweet } end end end X ML <?xml version="1.0" encoding="UTF-8"?> ...
REQUEST REQUEST C ont r oll e r A c tio n s /a pp /c ont r oll e rs /t wee t s _c ont r oll e r .r b class TweetsController < ApplicationController L ist all twe e ts def index V iew s def show S how a s i ngl e tw e et def new S how a n e w t w eet for m def edit S how an e dit twe e t f o rm def create C rea t e a new twe e t def update U pda t e a twe e t def destroy D ele t e a twe e t end
EDIT EDIT C ont r oll e r A c tio n s /a pp /c ont r oll e rs /t wee t s _c ont r oll e r .r b class TweetsController < ApplicationController L ist all twe e ts def index def show S how a s i ngl e tw e et def new S how a n e w t w eet for m def edit S how an e dit twe e t f o rm def create C rea t e a new twe e t def update U pda t e a twe e t def destroy D ele t e a twe e t end
EDIT EDIT T he E dit Act i on /a pp /c ont r oll e rs /t wee t s _c ont r oll e r .r b def edit @tweet = Tweet.find(params[:id]) end zombie_twitter app views tweets edit.html.erb
EDIT EDIT A ddi n g S o me A uth o riz a tio n
DELETE DELETE A ddi n g S o me A uth o riz a tio n
DELETE DELETE A ddi n g S o me A uth o riz a tio n
REQUEST REQUEST R edi r ect and Fla s h /a pp /c ont r oll e rs /t wee t s _c ont r oll e r .r b class TweetsController < ApplicationController def edit @tweet = Tweet.find(params[:id]) if session[:zombie_id] != @tweet.zombie_id flash[:notice] ="Sorry, you can’t edit this tweet" redirect_to(tweets_path ) end end end session Works like a per user hash flash[:notice] To send messages to the user redirect <path> To redirect the request
REQUEST REQUEST R edi r ect and Fla s h /a pp /c ont r oll e rs /t wee t s _c ont r oll e r .r b class TweetsController < ApplicationController def edit @tweet = Tweet.find(params[:id]) if session[:zombie_id] != @tweet.zombie_id flash[:notice] ="Sorry, you can’t edit this tweet" redirect_to(tweets_path ) end end F las h + Red i rec t end A lte r nat e Re c ipe : redirect_to(tweets_path, :notice => "Sorry, you can’t edit this tweet")
N oti c e f o r L a you t s /a pp /v iew s/l ayo u ts /a ppl i cat i on .h tml .e rb <!DOCTYPE html> <head> <title>Twitter for Zombies</title> </head> <body> <img src="/images/twitter.png" /> <%= yield %> </body> </html>
N oti c e f o r L a you t s /a pp /v iew s/l ayo u ts /a ppl i cat i on .h tml .e rb <!DOCTYPE html> <head> <title>Twitter for Zombies</title> </head> <body> <img src="/images/twitter.png" /> <% if flash[:notice] %> <div id="notice"><%= flash[:notice] %></div> <% end %> <%= yield %> </body> </html>
DELETE DELETE A ddi n g S o me A uth o riz a tio n
DELETE DELETE A ddi n g S o me A uth o riz a tio n
REQUEST REQUEST C ont r oll e r A c tio n s /a pp /c ont r oll e rs /t wee t s _c ont r oll e r .r b class TweetsController < ApplicationController L ist all twe e ts def index V iew s S how a s i ngl e tw e et def show def new S how a n e w t w eet for m def edit S how an e dit twe e t f o rm def create C rea t e a new twe e t def update U pda t e a twe e t def destroy D ele t e a twe e t end
Recommend
More recommend