Show a tweet <% ... %> Evaluate Ruby <%= ... %> Eval and print result /app/views/tweets/show.html.erb <!DOCTYPE html> <html> <head><title>Twitter for Zombies</title></head> <body> FYI, This code <img src="/images/twitter.png" /> sucks a little.. <% tweet = Tweet.find(1) %> (for 2 reasons) <h1><%= tweet.status %></h1> <p>Posted by <%= tweet.zombie.name %></p> </body></html>
Show a tweet /app/views/layouts/application.html.erb <!DOCTYPE html> <html> <head><title>Twitter for Zombies</title></head> <body> <img src="/images/twitter.png" /> <%= yield %> </body></html> /app/views/tweets/show.html.erb <% tweet = Tweet.find(1) %> <h1><%= tweet.status %></h1> <p>Posted by <%= tweet.zombie.name %></p>
Views zombie_twitter app views The main layout layouts application.html.erb zombies List all tweets tweets index.html.erb View a tweet show.html.erb
Additional Layout Components /app/views/layouts/application.html.erb <!DOCTYPE html> <html> <head> <title>Twitter for Zombies</title> </head> <body> <img src="/images/twitter.png" /> <%= yield %> </body></html>
Additional Layout Components /app/views/layouts/application.html.erb <!DOCTYPE html> <html> <head> <title>Twitter for Zombies</title> <%= stylesheet_link_tag :all %> <%= javascript_include_tag :defaults %> <%= csrf_meta_tag %> </head> <body> <img src="/images/twitter.png" /> <%= yield %> </body></html>
Additional Layout Components <%= stylesheet_link_tag :all %> Include all stylesheets zombie_twitter public stylesheets Renders <link href="/stylesheets/scaffold.css" media="screen" rel="stylesheet" type="text/css" /> <%= javascript_include_tag :defaults %> <%= csrf_meta_tag %>
Additional Layout Components <%= stylesheet_link_tag :all %> <%= javascript_include_tag :defaults %> Include default JS zombie_twitter public javascripts Renders Prototype Javascript <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> Framework <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> <%= csrf_meta_tag %>
Additional Layout Components <%= stylesheet_link_tag :all %> <%= javascript_include_tag :defaults %> <%= csrf_meta_tag %> t e r S i k e H a c i e m b Z o Your Site m " > . c o i t e u r s y o : / / t t p = " h g e t t a r r m < f o Think comment spam Renders <meta name="csrf-param" content="authenticity_token"/> <meta name="csrf-token" content="I+d..jI="/> Automatically adds this to forms
Root path and images http://ZombieTwitter.com/[something] c i zombie_twitter l b u p / s l i a R n i public o s t t s i x o e g stylesheets f e I s i w r e h t javascripts o , t i e s u images Example <img src="/images/twitter.png" />
Adding a Link Make zombie a link /app/views/tweets/show.html.erb ... <p>Posted by <%= tweet.zombie.name %></p> <%= link_to tweet.zombie.name , zombie_path(tweet.zombie) %> Link Text Link Path (URL) Renders <a href="/zombies/1">Ash</a> We can also write as <%= link_to tweet.zombie.name, tweet.zombie %> Link Text Object to Show
Method What options can you use with it? link_to 1. Look in the source Command Line git clone http://github.com/rails/rails.git cd rails Open your editor and search for “def link_to”
Method What options can you use with it? link_to 2. Look at api.rubyonrails.org (and search for link_to)
http://api.rubyonrails.org
Method What options can you use with it? link_to 3. API Dock (online Ruby and Rails docs) http://apidock.com/rails
Method What options can you use with it? link_to 4. Rails Searchable API Doc (local download or online) http://railsapi.com/
http://railsapi.com/
<%= link_to @tweet.zombie.name, @tweet.zombie, :confirm => "Are you sure?" %>
Views zombie_twitter app views The main layout layouts application.html.erb zombies List all tweets tweets index.html.erb View a tweet show.html.erb
Views List Tweets /app/views/tweets/index.html.erb <h1>Listing tweets</h1> <table> <tr> <th>Status</th> What they return <th>Zombie</th> </tr> Tweet <% Tweet.all.each do |tweet| %> class <tr> <td><%= tweet.status %></td> Tweet.all array of tweets <td><%= tweet.zombie.name %></td> </tr> tweet single tweet <% end %> </table>
Views Create Links /app/views/tweets/index.html.erb <% Tweet.all.each do |tweet| %> <tr> <td><%= tweet.status %></td> <td><%= tweet.zombie.name %></td> </tr> <% end %>
Views Create Links /app/views/tweets/index.html.erb <% Tweet.all.each do |tweet| %> <tr> <td><%= link_to tweet.status , tweet %></td> <td><%= tweet.zombie.name %></td> </tr> <% end %>
Views Create Links /app/views/tweets/index.html.erb <% Tweet.all.each do |tweet| %> <tr> <td><%= link_to tweet.status , tweet %></td> <td><%= tweet.zombie.name , tweet.zombie link_to %></td> </tr> <% end %>
Views Empty Table? <% Tweet.all .each do |tweet| %> <tr> <td><%= link_to tweet.status, tweet %></td> <td><%= link_to tweet.zombie.name, tweet.zombie %></td> </tr> <% end %>
Views Empty Table? <% Tweet.all .each do |tweet| %> ... <% end %>
Views Empty Table? <% tweets = tweets Tweet.all %> <% .each do |tweet| %> ... <% end %>
Views Empty Table? <% tweets = Tweet.all %> <% tweets .each do |tweet| %> ... <% end %> <% if tweets.size == 0 %> <em>No tweets found</em> <% end %>
Views Empty Table? <% tweets = Tweet.all %> <% tweets .each do |tweet| %> ... <% end %> <% if tweets.empty? %> <em>No tweets found</em> <% end %>
Edit & Delete Links? <% 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 %>
All links for Tweets <%= link_to "<link text>", <code> %> Action Code The URL Generated tweets_path List all tweets /tweets new_tweet_path New tweet form /tweets/new T h e s e p a t h tweet = Tweet.find(1) s n e e d a t w e e t Action Code The URL Generated tweet Show a tweet /tweets/1 edit_tweet_path(tweet) Edit a tweet /tweets/1/edit tweet, :method => :delete Delete a tweet /tweets/1
Zombie lab 3 Zombie lab 3
An Introduction to Rails g P o l l a c k t h G r e g w i Episode # 4
brains Chapter 4 v Controllers must be eaten
Views Our Application Stack Views Controllers Models 4 Components
Show a tweet /app/views/tweets/show.html.erb <!DOCTYPE html> <html> <head><title>Twitter for Zombies</title></head> <body> FYI, This code <img src="/images/twitter.png" /> sucks a little.. <% tweet = Tweet.find(1) %> (we’ll fix it later) <h1><%= tweet.status %></h1> <p>Posted by <%= tweet.zombie.name %></p> </body></html>
zombie_twitter Request app controllers /tweets/1 tweets_controller.rb /app/controllers/tweets_controller.rb Controller /app/views/tweets/show.html.erb <% tweet = Tweet.find(1) %> <h1><%= tweet.status %></h1> <p>Posted by <%= tweet.zombie.name %></p>
zombie_twitter app Request controllers tweets /tweets/1 tweets_controller.rb tweets /app/controllers/tweets_controller.rb Controller /app/views/tweets/show.html.erb tweets <% 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 /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 def show end end /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 def show show end end /app/views/tweets/show.html.erb show <% 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 def show end end /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 def show tweet = Tweet.find(1 ) end end What about /app/views/tweets/show.html.erb variable scope? <h1><%= tweet.status %></h1> <p>Posted by <%= tweet.zombie.name %></p>
Instance Variable Request /tweets/1 @ /app/controllers/tweets_controller.rb class TweetsController < ApplicationController def show tweet = Tweet.find(1 ) @ end end /app/views/tweets/show.html.erb <h1><%= @ tweet.status %></h1> <p>Posted by <%= @ tweet.zombie.name %></p>
Render Request /tweets/1 /app/controllers/tweets_controller.rb class TweetsController < ApplicationController def show tweet = Tweet.find(1 ) @ end end /app/views/tweets/ status .html.erb <h1><%= @ tweet.status %></h1> <p>Posted by <%= @ tweet.zombie.name %></p>
Render Request /tweets/1 /app/controllers/tweets_controller.rb class TweetsController < ApplicationController def show tweet = Tweet.find(1 ) @ render :action => 'status' end end /app/views/tweets/status.html.erb <h1><%= @ tweet.status %></h1> <p>Posted by <%= @ tweet.zombie.name %></p>
Render Request /tweets/1 /tweets/2 /app/controllers/tweets_controller.rb class TweetsController < ApplicationController /tweets/3 def show /tweets/4 tweet = Tweet.find(1 ) @ /tweets/5 render :action => 'status' end end /app/views/tweets/status.html.erb <h1><%= @ tweet.status %></h1> <p>Posted by <%= @ tweet.zombie.name %></p>
Render Request /tweets/1 /tweets/2 /app/controllers/tweets_controller.rb class TweetsController < ApplicationController /tweets/3 def show /tweets/4 tweet = Tweet.find(1 ) @ /tweets/5 render :action => 'status' end end params[:id] /app/views/tweets/status.html.erb <h1><%= @ tweet.status %></h1> <p>Posted by <%= @ tweet.zombie.name %></p>
Render Request params = { :id => "1" } /tweets/1 /tweets/2 /app/controllers/tweets_controller.rb class TweetsController < ApplicationController /tweets/3 def show /tweets/4 tweet = Tweet.find(params[:id] ) @ /tweets/5 render :action => 'status' end end params[:id] /app/views/tweets/status.html.erb <h1><%= @ tweet.status %></h1> <p>Posted by <%= @ tweet.zombie.name %></p>
Parameters Request params = { :status => "I’m dead" } @ tweet = Tweet.create(:status => ) params[:status] params = {:tweet => { :status => "I’m dead" }} @ tweet = Tweet.create(:status => params[:tweet][:status]) We can also write as @ tweet = Tweet.create(params[:tweet])
Request /tweets/1 xml? json? xml <?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> json {"tweet":{"id":1,"status":"Where can I get a good bite to eat?","zombie_id":1}}
Request class TweetsController < ApplicationController /tweets/1 def show @tweet = Tweet.find(params[:id]) xml? json? respond_to do |format| format.html # show.html.erb format.xml { render :xml => @tweet } format.json { render :json => @tweet } end end /tweets/1. xml <?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> /tweets/1. json {"tweet":{"id":1,"status":"Where can I get a good bite to eat?","zombie_id":1}}
Controller Actions Request /app/controllers/tweets_controller.rb class TweetsController < ApplicationController List all tweets def index Show a single tweet def show Show a new tweet form def new Show an edit tweet form def edit View Create a new tweet def create Update a tweet def update Delete a tweet def destroy end
Adding some authorization def edit
Adding some authorization /app/controllers/tweets_controller.rb def edit @tweet = Tweet.find(params[:id]) end zombie_twitter app views tweets edit.html.erb
Adding some authorization
Adding some authorization
Adding some authorization
Redirect and Flash Request /app/controllers/tweets_controller.rb 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 Works like a per user hash session To send messages to the user flash[:notice] To redirect the request redirect <path>
Redirect and Flash Request /app/controllers/tweets_controller.rb class TweetsController < ApplicationController def edit @tweet = Tweet.find(params[:id]) if session[:zombie_id] != @tweet.zombie_id , redirect_to(tweets_path :notice => "Sorry, you can’t edit this tweet") end end end A l t e r n a t e S y n t a x c o m b i n i n g r e d i r e c t & f l a s h
Notice for Layouts /app/views/layouts/application.html.erb <!DOCTYPE html> <html> <head> <title>Twitter for Zombies</title> <%= stylesheet_link_tag :all %> <%= javascript_include_tag :defaults %> <%= csrf_meta_tag %> </head> <body> <img src="/images/twitter.png" /> <%= yield %> </body></html>
Recommend
More recommend