cheap fast and good
play

Cheap, Fast, and Good You can have it all with Ruby on Rails Brian - PowerPoint PPT Presentation

Cheap, Fast, and Good You can have it all with Ruby on Rails Brian McCallister brianm@ninginc.com http://www.ning.com/ (c) 2005, Brian McCallister What is Ruby? Dynamic and Interpreted Strong support for OO programming Everything


  1. Cheap, Fast, and Good You can have it all with Ruby on Rails Brian McCallister brianm@ninginc.com http://www.ning.com/ (c) 2005, Brian McCallister

  2. What is Ruby? • Dynamic and Interpreted • Strong support for OO programming • Everything is an object ( 2.next == 3 ) • Strong support for functional-style programming • Blocks, closures, first-class functions • Child of Perl and Smalltalk (c) 2005, Brian McCallister

  3. What is Rails? • Model-2 Web Framework • Object/Relational Mapping Library • SOAP Producer/Consumer Framework • Email Templating/Mailing Library • Code Generator • Very Rapidly Evolving! (c) 2005, Brian McCallister

  4. Principles Involved • Less Code • Convention over Configuration • Proximity • Least Surprise • Make components play nicely together! (c) 2005, Brian McCallister

  5. Action Pack That Web Stuff (c) 2005, Brian McCallister

  6. Request Cycle (c) 2005, Brian McCallister

  7. Some Code require 'date' class AggregateController < ApplicationController model :entry def for_date date = DateTime.parse @params[:date] logger.debug "for_date: #{date}" @entries = Entry.on_date date end # Additional actions removed for slide’s benefit` end (c) 2005, Brian McCallister

  8. Action require 'date' class AggregateController < ApplicationController model :entry Action def for_date date = DateTime.parse @params[:date] logger.debug "for_date: #{date}" @entries = Entry.on_date date end # Additional actions removed for slide’s benefit` end (c) 2005, Brian McCallister

  9. Controller Controller require 'date' class AggregateController < ApplicationController model :entry Action def for_date date = DateTime.parse @params[:date] logger.debug "for_date: #{date}" @entries = Entry.on_date date end Action def list_entries @entries = Entry.find_all end # Additional actions removed for slide’s benefit` end (c) 2005, Brian McCallister

  10. How We Got Here ActionController::Routing::Routes.draw do |map| # map.connect ':controller/service.wsdl', # :action => 'wsdl' map.connect 'wombat/is/friendly', :controller => :feeds, :action => :list map.connect '', :controller => :feeds, :action => :list # Default Route map.connect ':controller/:action/:id' end (c) 2005, Brian McCallister

  11. routes.rb ActionController::Routing::Routes.draw do |map| http://localhost/wombat/is/friendly # map.connect ':controller/service.wsdl', # :action => 'wsdl' map.connect 'wombat/is/friendly', :controller => :feeds, :action => :list map.connect '', :controller => :feeds, :action => :list # Default Route map.connect ':controller/:action/:id' end (c) 2005, Brian McCallister

  12. routes.rb ActionController::Routing::Routes.draw do |map| # map.connect ':controller/service.wsdl', # :action => 'wsdl' map.connect 'wombat/is/friendly', http://localhost/ :controller => :feeds, :action => :list map.connect '', :controller => :feeds, :action => :list # Default Route map.connect ':controller/:action/:id' end (c) 2005, Brian McCallister

  13. routes.rb ActionController::Routing::Routes.draw do |map| # map.connect ':controller/service.wsdl', # :action => 'wsdl' map.connect 'wombat/is/friendly', :controller => :feeds, :action => :list map.connect '', :controller => :feeds, http://localhost/feeds/list :action => :list # Default Route map.connect ':controller/:action/:id' end (c) 2005, Brian McCallister

  14. Show Us Something! <h1>Recent Stories...</h1> <table class="entryTable" > <% for entry in @entries %> <tr> <td class="entryTitle"> <%= entry.feed.title + ": " + entry.title %> </td> </tr> <tr> <td class="entryBody"> <%= entry.body %> </td> </tr> <% end %> </table> (c) 2005, Brian McCallister

  15. Directives <h1>Recent Stories...</h1> <table class="entryTable" > <% for entry in @entries %> <tr> <td class="entryTitle"> <%= entry.feed.title + ": " + entry.title %> </td> </tr> <tr> <td class="entryBody"> <%= entry.body %> </td> </tr> <% end %> </table> (c) 2005, Brian McCallister

  16. Expressions <h1>Recent Stories...</h1> <table class="entryTable" > <% for entry in @entries %> <tr> <td class="entryTitle"> <%= entry.feed.title + ": " + entry.title %> </td> </tr> <tr> <td class="entryBody"> <%= entry.body %> </td> </tr> <% end %> </table> (c) 2005, Brian McCallister

  17. Helpers & Partials <h1>Recent Stories...</h1> my_view.rhtml <%= render_partial "entry_list", :stories => @entries %> <table class="entryTable" > _entry_list.rhtml <% for entry in stories %> <tr> <td class="entryTitle"> <%= entry.feed.title + ": " + entry.title %> </td> </tr> <tr> <td class="entryBody"> <%= entry.body %> </td> </tr> <% end %> </table> (c) 2005, Brian McCallister

  18. Helpers <h1>Recent Stories...</h1> my_view.rhtml <%= render_partial "entry_list", :stories => @entries %> <table class="entryTable" > _entry_list.rhtml <% for entry in stories %> <tr> <td class="entryTitle"> <%= entry.feed.title + ": " + entry.title %> </td> </tr> <tr> <td class="entryBody"> <%= entry.body %> </td> </tr> <% end %> </table> (c) 2005, Brian McCallister

  19. Parameterized Partials <h1>Recent Stories...</h1> my_view.rhtml <%= render_partial "entry_list", :stories => @entries %> <table class="entryTable" > _entry_list.rhtml <% for entry in stories %> <tr> <td class="entryTitle"> <%= entry.feed.title + ": " + entry.title %> </td> </tr> <tr> <td class="entryBody"> <%= entry.body %> </td> </tr> <% end %> </table> (c) 2005, Brian McCallister

  20. Picking Views • Default Views • The View for that one over there... • Named Views • Something completely different? (c) 2005, Brian McCallister

  21. Default Views require 'date' class AggregateController < ApplicationController model :entry aggregate/for_date.rhtml def for_date date = DateTime.parse @params[:date] logger.debug "for_date: #{date}" @entries = Entry.on_date date end # Additional actions removed for slide’s benefit` end (c) 2005, Brian McCallister

  22. render_* require 'date' class AggregateController < ApplicationController model :entry def today @entries = Entry.on_date Date.today render_action :list_entries end aggregate/list_entries.rhtml def list_entries @entries = Entry.find_all end # Additional actions removed for slide’s benefit` end (c) 2005, Brian McCallister

  23. Named Views require 'date' class AggregateController < ApplicationController model :entry def another_one @entries = Entry.find_all render :a_template a_template.rhtml end # Additional actions removed for slide’s benefit` end (c) 2005, Brian McCallister

  24. Raw Rendering require 'date' class AggregateController < ApplicationController model :entry def direct_write render_text "Hello World!" end No template! # Additional actions removed for slide’s benefit` end (c) 2005, Brian McCallister

  25. Additional Options • send_data • send_file • render_to_string • render_nothing • render_text with a block • redirect_to • redirect_to_url • redirect_to_path • builders • and more! (c) 2005, Brian McCallister

  26. Helpers • Programmatic output generation • Global, Controller Specific, Importable • Like tag libraries • kind of • Lots of built-ins (c) 2005, Brian McCallister

  27. #{Controller}Helper module AggregateHelper def say_hello(name) Helper "<h1>Hello, #{name}" end end <h1>Recent Stories...</h1> Usage <%= say_hello "Brian" %> <table> <% for entry in @entries %> ... (c) 2005, Brian McCallister

  28. Rules, well Suggestions • #{Controller}Helper applied from matching Controller • ApplicationHelper available everywhere • Declare reliance on a specific Helper from any Controller • Rarely need to do this, though (c) 2005, Brian McCallister

  29. Action Pack Magic 3 • Controllers • Helpers • Views • Things not discussed: • Components • Caching • Filters (c) 2005, Brian McCallister

  30. Active Record You get the data from the database and shake it all about... (c) 2005, Brian McCallister

  31. Active Record Basics • Not Required! • One Row == One Instance • Dynamic Properties by Default • Query by SQL or Criteria • Including joins • PostgreSQL, MySQL, Oracle, DB2 ... more (c) 2005, Brian McCallister

  32. The Schema (c) 2005, Brian McCallister

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend