Ruby On Rails Intro

2,679 views
2,564 views

Published on

Intro to Ruby and Rails, including development environment, MVC, erb and ActiveRecord

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,679
On SlideShare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
28
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Ruby On Rails Intro

  1. 1. Ruby on RailsAn Introduction<br />Sarah Allen<br />Blazing Cloud <br />
  2. 2. Ruby on Rails history<br />Ruby on Rails was extracted from 37signals’ Basecamp by David Heinemeier Hansson (DHH)<br />July 2004: first released as open source <br />Feb 2005: first external commit rights <br />Oct 2007: ships with Mac OS X v10.5 "Leopard”<br />
  3. 3. Rails Philosophy<br /> Opinionated<br /> Convention over configuration<br /> DRY (Don't Repeat Yourself)<br />less code means it's easier to maintain & modify<br /> Test Driven Development (TDD)<br /> Minimal code - maximal effect<br />
  4. 4. What you will learn<br />Ruby<br />Language concepts<br />Language syntax<br />Common patterns<br />Rails Framework: creating web applications<br />Scaffold<br />Model, View, Controllers, Routes<br />SQL Queries, log files and debugging<br />Associations<br />
  5. 5. How you will learn<br />Exploration: experiment, play<br />Test-Driven Development (TDD)<br />Initially as a learning methodology<br />Later as a development methodology<br />Ask questions<br />Learn to find your own answers<br />
  6. 6. Class Structure<br />Talk<br />Live Coding Demonstrations<br />In-class coding<br />
  7. 7. Prerequistes <br />Core dependencies: Ruby, Rails, DB<br />Tools: vcs, IDE, test frameworks, deployment<br />
  8. 8. Core Dependencies <br />Ruby<br />Rails<br />Rake<br />Database<br />
  9. 9. Ruby <br />Rails is a framework <br />written in the Ruby language.<br />Great Rails developers <br />are great Ruby developers.<br />
  10. 10. The Ruby Language<br />Originally by Yukihiro "Matz" Matsumoto<br />“Ruby is designed for programmer productivity and fun, following the principles of good user interface design. He stresses that systems design needs to emphasize human, rather than computer, needs.”<br />http://en.wikipedia.org/wiki/Ruby_(programming_language)#History<br />Ruby 1.0 was released in 1996. <br />
  11. 11. Ruby <br />ruby -v<br />1.8.6 or 1.8.7<br />
  12. 12. Ruby Versions<br />ruby -v<br />1.8.6 – most common<br />1.8.7 – some 1.9 features, very compatible<br />1.9.1 – latest version, many VMs, Rails 2.3<br />YARV (yet another Ruby VM) faster than MRI<br />JRuby (Java)<br />Rubinius (pure ruby) <br />IronRuby (.NET)<br />MacRuby, HotCocoa<br />HotRuby/RubyJS (Flash/Javascript)<br />
  13. 13. Ruby Gems<br />A gem is a ruby library.<br />gem –v 1.3.5 or higher<br />gem list[sudo] gem install<br />
  14. 14. Rails<br />Rails is distributed as a Ruby gem.<br />gem list rails 2.3.4 or higher<br />[sudo] gem install rails<br />
  15. 15. rake<br />Rake is “make” for Ruby. Rails requires rake.<br />Rake is distrubted as a gem.<br />gem list rake0.8.7 or higher<br />[sudo] gem install rake<br />
  16. 16. Database<br />SQLite for class with sqlite3-ruby gem<br />Other databases:<br />MySQL<br />PostgreSQL<br />Oracle<br />SQL Server<br />SyBase<br />DB2<br />
  17. 17. Tools <br />Source Code Control with Git<br />Terminal / git bash on windows<br />Editor / IDE<br />Test Frameworks<br />Heroku for Easy Deployment<br />
  18. 18. git<br />Git is for source code control.<br />which git (mac, unix)<br />git bash on windows<br />Why Git?<br />Most Ruby and Rails developers use git<br />Eco-system of tools<br />Modern Source Code Control<br />
  19. 19.
  20. 20. command line<br />Mac/Unix Terminal<br />GitBash on Windows<br />
  21. 21. Editor / IDE<br />RubyMine<br />TextMate (Mac-only)<br />Komodo (free)<br />
  22. 22. Test Frameworks<br />gem list rspec<br />rspec (1.3.0)<br />rspec-rails (1.3.2)<br />gem list cucumber<br />cucumber (0.6.2)<br />cucumber-rails (0.2.4)<br />
  23. 23. Heroku<br />Simple cloud hosting<br />Web sign-up for free account: heroku.com<br /> [sudo] gem install heroku<br />
  24. 24. Prerequisites <br />Core dependencies<br />Ruby<br />Ruby Gems<br />Rails (gem)<br />Rake<br />Database<br />Tools<br />Git<br />Terminal / git bash on windows<br />Test Frameworks<br />rspec, rspec-rails<br />cucumber, cucumber-rails<br />Heroku (for deployment)<br />
  25. 25. Let’s Get Started<br />
  26. 26. Lets get started<br />IRB: InteractiveRuBy<br />>> 4<br />>> 4 + 4<br />
  27. 27. Everything is an object<br />“test”.upcase<br />“test”.class<br />“test”.methods<br />
  28. 28. Everything evaluates to something<br />2 + 2<br />(2+2).zero?<br />
  29. 29. Methods are Messages<br />thing.do(4)<br />thing.do 4<br />thing.send “do”, 4<br />
  30. 30. Operators are Methods<br />thing.do 4<br />thing.do(4)<br />thing.send “do”, 4<br />1 + 2<br />1.+(2)<br />1.send "+", 2<br />
  31. 31. Defining Classes<br />Let’s write some code!<br />
  32. 32. Test-First Learning<br />Similar methodology to TDDwith a different purpose and workflow<br />Teacher writes the test<br />Student implements the code<br />
  33. 33. Test-Driven Development<br />Design<br />Focus / Project Management<br />Creation of Tests<br />
  34. 34. Introduction to Rspec<br />Describe the feature<br />Verify expectation<br />
  35. 35.
  36. 36. Scaffold<br />Model<br />app/models/person.rb<br />db/migrate/20090611073227_create_people.rb<br />4 views<br />app/views/people/index.html.erb<br />app/views/people/show.html.erb<br />app/views/people/new.html.erb<br />app/views/people/edit.html.erb<br />Controller<br />app/controllers/people_controller.rb<br />route map.resources :people<br />
  37. 37. MVC<br />Model: ActiveRecord<br />Represents what is in the database<br />View: ActionView, erb<br />Model rendered as HTML<br />Controller: ActionController<br />Receives HTTP actions (GET, POST, PUT, DELETE)<br />Decides what to do, typically rendering a view<br />
  38. 38. MVC<br />
  39. 39. views<br /><% @people.each do |person| %><br /><tr><br /> <td><%=h person.first_name %></td><br /> <td><%=h person.last_name %></td><br /> <td><%=h person.present %></td><br /></tr><br /><% end %><br />
  40. 40. View Exercise<br />1. On the main people page  <br /> a. Change “Listing people” to “My Class List”  <br /> b. Remove the “Present” column  <br />2. When you click “show,” the page should read “Joy McDonald was not present at class” <br /> or “Bob Smith was present at class”   <br />
  41. 41. ActiveRecord<br />p = new Person<br />p = new Person(:first => "May", :last => "Fong")<br />p.save<br />p.save!<br />Person.create(:first => "May", :last => "Fong")<br />Person.create!(:first => "May", :last => "Fong")<br />
  42. 42. Safe from SQL injection<br />class User < ActiveRecord::Base <br /> def self.authenticate_unsafely(user_name, password) <br /> find(:first, :conditions => <br /> "user_name = '#{user_name}' AND password = '#{password}'") <br /> end <br /> def self.authenticate_safely(user_name, password) <br /> find(:first, :conditions => <br /> [ "user_name = ? AND password = ?", user_name, password ]) <br /> end <br /> def self.authenticate_safely_simply(user_name, password) <br /> find(:first, :conditions => <br /> { :user_name => user_name, :password => password }) <br /> end <br /> end <br />

×