an introduction to rails
play

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


  1. 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>

  2. 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>

  3. 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

  4. 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>

  5. 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>

  6. 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 %>

  7. 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 %>

  8. 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

  9. 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" />

  10. 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

  11. 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”

  12. Method What options can you use with it? link_to 2. Look at api.rubyonrails.org (and search for link_to)

  13. http://api.rubyonrails.org

  14. Method What options can you use with it? link_to 3. API Dock (online Ruby and Rails docs) http://apidock.com/rails

  15. Method What options can you use with it? link_to 4. Rails Searchable API Doc (local download or online) http://railsapi.com/

  16. http://railsapi.com/

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

  18. 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

  19. 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>

  20. 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 %>

  21. 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 %>

  22. 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 %>

  23. 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 %>

  24. Views Empty Table? <% Tweet.all .each do |tweet| %> ... <% end %>

  25. Views Empty Table? <% tweets = tweets Tweet.all %> <% .each do |tweet| %> ... <% end %>

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

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

  28. 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 %>

  29. 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

  30. Zombie lab 3 Zombie lab 3

  31. An Introduction to Rails g P o l l a c k t h G r e g w i Episode # 4

  32. brains Chapter 4 v Controllers must be eaten

  33. Views Our Application Stack Views Controllers Models 4 Components

  34. 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>

  35. 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>

  36. 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>

  37. 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>

  38. 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>

  39. 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>

  40. 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>

  41. 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>

  42. 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>

  43. 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>

  44. 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>

  45. 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>

  46. 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>

  47. 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>

  48. 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])

  49. 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}}

  50. 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}}

  51. 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

  52. Adding some authorization def edit

  53. Adding some authorization /app/controllers/tweets_controller.rb def edit @tweet = Tweet.find(params[:id]) end zombie_twitter app views tweets edit.html.erb

  54. Adding some authorization

  55. Adding some authorization

  56. Adding some authorization

  57. 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>

  58. 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

  59. 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