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