Ruby on RailsAn IntroductionSarah AllenBlazing Cloud
Ruby on Rails historyRuby 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”
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
Prerequistes	Core dependencies: Ruby, Rails, DBTools: vcs, IDE, test frameworks, deployment
Core Dependencies	RubyRailsRakeDatabase
Ruby	Rails is a framework written in the Ruby language.Great Rails developers are great Ruby developers.
Ruby	ruby -v1.8.6 or 1.8.7
Ruby	GemsA gem is a ruby library.gem –v   1.3.5 or highergem list[sudo] gem install
RailsRails is distributed as a Ruby gem.gem list rails   2.3.4 or higher[sudo] gem install rails
rakeRake is “make” for Ruby.  Rails requires rake.Rake is distrubted as a gem.gem list rake0.8.7 or higher[sudo] gem install rake
Tools	Source Code Control with Git Terminal / git bash on windowsEditor / IDETest FrameworksHerok for Easy Deployment
gitGit is for source code control.which git (mac, unix)git bash on windowsWhy Git?Most Ruby and Rails developers use gitEco-system of toolsModern Source Code Control
command lineMac/Unix TerminalGitBash on Windows
Editor / IDERubyMineTextMate (Mac-only)Komodo (free)
Test Frameworksgem list rspecrspec (1.3.0)rspec-rails (1.3.2)gem list cucumbercucumber (0.6.2)cucumber-rails (0.2.4)
HerokuSimple cloud hostingWeb sign-up for free account: heroku.com	[sudo] gem install heroku
Prerequisites	Core dependenciesRubyRuby GemsRails (gem)DatabaseToolsGit Terminal / git bash on windowsRake (gem)Test Frameworksrspec, rspec-railscucumber, cucumber-railsHeroku (for deployment)
Let’s Get Started
ScaffoldModelapp/models/person.rbdb/migrate/20090611073227_create_people.rb4 viewsapp/views/people/index.html.erbapp/views/people/show.html.erbapp/views/people/new.html.erbapp/views/people/edit.html.erbControllerapp/controllers/people_controller.rbroute map.resources :people
MVCModel: ActiveRecordRepresents what is in the databaseView: ActionView, erbModel rendered as HTMLController: ActionControllerReceives HTTP actions (GET, POST, PUT, DELETE)Decides what to do, typically rendering a view
MVC
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 %>
View Exercise1. 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”  
ActiveRecordp = new Personp = new Person(:first => "May", :last => "Fong")p.savep.save!Person.create(:first => "May", :last => "Fong")Person.create!(:first => "May", :last => "Fong")
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

Intro to Rails and MVC