160
Hello

A Z Introduction To Ruby On Rails

Embed Size (px)

Citation preview

Page 1: A Z Introduction To Ruby On Rails

Hello

Page 2: A Z Introduction To Ruby On Rails

Robert Dempsey

Page 3: A Z Introduction To Ruby On Rails
Page 4: A Z Introduction To Ruby On Rails
Page 5: A Z Introduction To Ruby On Rails

adsdevshop.com

Page 6: A Z Introduction To Ruby On Rails

adsdevshop.com/apps

Page 7: A Z Introduction To Ruby On Rails

rdempsey

Page 8: A Z Introduction To Ruby On Rails
Page 9: A Z Introduction To Ruby On Rails

A-Z Intro ToRuby on Rails

Page 10: A Z Introduction To Ruby On Rails

AlwaysBeLearning

Page 11: A Z Introduction To Ruby On Rails
Page 12: A Z Introduction To Ruby On Rails

Teach Others

Page 13: A Z Introduction To Ruby On Rails
Page 14: A Z Introduction To Ruby On Rails

Be Bold

Page 15: A Z Introduction To Ruby On Rails

Whatʼs Going On• Introduction to Ruby

• Introduction to Rails

• Hands-on Lab

• Breaks interspersed

Page 16: A Z Introduction To Ruby On Rails

Ruby

Page 17: A Z Introduction To Ruby On Rails
Page 18: A Z Introduction To Ruby On Rails

1995

Page 19: A Z Introduction To Ruby On Rails

2006

Page 20: A Z Introduction To Ruby On Rails

Perl SmallTalk Eiffel Ada+ Lisp Ruby

Page 21: A Z Introduction To Ruby On Rails

#10 baby!

Page 22: A Z Introduction To Ruby On Rails
Page 23: A Z Introduction To Ruby On Rails

5.times { print “We love Ruby” }

Page 24: A Z Introduction To Ruby On Rails

class Numeric def plus(x) self.+(x) endend

y = 5.plus 6# y is now equal to 11

Page 25: A Z Introduction To Ruby On Rails
Page 26: A Z Introduction To Ruby On Rails

Rails

Page 27: A Z Introduction To Ruby On Rails
Page 28: A Z Introduction To Ruby On Rails

2005

Page 29: A Z Introduction To Ruby On Rails

Image copyright 2008 Yoshiko314 (Flickr)

Page 30: A Z Introduction To Ruby On Rails
Page 31: A Z Introduction To Ruby On Rails
Page 32: A Z Introduction To Ruby On Rails
Page 33: A Z Introduction To Ruby On Rails

MVC

Page 34: A Z Introduction To Ruby On Rails

ModelViewController

Page 35: A Z Introduction To Ruby On Rails

ModelViewController

Page 36: A Z Introduction To Ruby On Rails

ModelViewController

Page 37: A Z Introduction To Ruby On Rails

ModelViewController

Page 38: A Z Introduction To Ruby On Rails

map.root :controller => ʻemployersʼ

Page 39: A Z Introduction To Ruby On Rails

Action HTTP Method URL XML

index GET /jobs /jobs.xml

show GET /jobs/1 /jobs/1.xml

new GET /jobs/new

edit GET /jobs/1;edit

create POST /jobs /jobs.xml

update PUT /jobs/1 /jobs/1.xml

destroy DELETE /jobs/1 /jobs/1.xml

Page 40: A Z Introduction To Ruby On Rails

Letʼs BuildSomething

Page 41: A Z Introduction To Ruby On Rails

The Project• Employers

• Jobs

Page 42: A Z Introduction To Ruby On Rails

rails jobby -d postgresql

Page 43: A Z Introduction To Ruby On Rails

rails jobby -d mysql

Page 44: A Z Introduction To Ruby On Rails

READMERakefileapp/config/db/doc/lib/log/public/script/test/tmp/vendor/

Page 45: A Z Introduction To Ruby On Rails

READMERakefileapp/config/db/doc/lib/log/public/script/test/tmp/vendor/

Page 46: A Z Introduction To Ruby On Rails

README

Rakefileapp/config/db/doc/lib/log/public/script/test/tmp/vendor/

Page 47: A Z Introduction To Ruby On Rails

READMERakefile

app/config/db/doc/lib/log/public/script/test/tmp/vendor/

Page 48: A Z Introduction To Ruby On Rails

app/ controllers/ helpers/ models/ views/

Page 49: A Z Introduction To Ruby On Rails

READMERakefileapp/

config/db/doc/lib/log/public/script/test/tmp/vendor/

Page 50: A Z Introduction To Ruby On Rails

READMERakefileapp/config/

db/doc/lib/log/public/script/test/tmp/vendor/

Page 51: A Z Introduction To Ruby On Rails

READMERakefileapp/config/db/

doc/lib/log/public/script/test/tmp/vendor/

Page 52: A Z Introduction To Ruby On Rails

READMERakefileapp/config/db/doc/

lib/log/public/script/test/tmp/vendor/

Page 53: A Z Introduction To Ruby On Rails

READMERakefileapp/config/db/doc/lib/

log/public/script/test/tmp/vendor/

Page 54: A Z Introduction To Ruby On Rails

READMERakefileapp/config/db/doc/lib/log/

public/script/test/tmp/vendor/

Page 55: A Z Introduction To Ruby On Rails

READMERakefileapp/config/db/doc/lib/log/public/

script/test/tmp/vendor/

Page 56: A Z Introduction To Ruby On Rails

READMERakefileapp/config/db/doc/lib/log/public/script/

test/tmp/vendor/

Page 57: A Z Introduction To Ruby On Rails

READMERakefileapp/config/db/doc/lib/log/public/script/test/

tmp/vendor/

Page 58: A Z Introduction To Ruby On Rails

READMERakefileapp/config/db/doc/lib/log/public/script/test/tmp/

vendor/

Page 59: A Z Introduction To Ruby On Rails

development: adapter: postgresql encoding: unicode database: jobby_development pool: 5 username: root password:

config/database.yml

Page 60: A Z Introduction To Ruby On Rails

development: adapter: mysql encoding: utf8 database: jobby_development pool: 5 username: root password: socket: /tmp/mysql.sock

config/database.yml

Page 61: A Z Introduction To Ruby On Rails

rake db:create

Page 62: A Z Introduction To Ruby On Rails

script/server

Page 63: A Z Introduction To Ruby On Rails
Page 64: A Z Introduction To Ruby On Rails

rm public/index.html

Page 65: A Z Introduction To Ruby On Rails

map.connect ʻ:controller/:action/:idʼ

map.connect ʻ:controller/:action/:id.:formatʼ

config/routes.rb

Page 66: A Z Introduction To Ruby On Rails

script/generate model Employer

Page 67: A Z Introduction To Ruby On Rails

script/generate scaffold Employer name:string address:string city:string state:string zipcode:string

Page 68: A Z Introduction To Ruby On Rails

exists app/models/ exists app/controllers/ exists app/helpers/ create app/views/employers exists app/views/layouts/ exists test/functional/ exists test/unit/ create test/unit/helpers/ exists public/stylesheets/ create app/views/employers/index.html.erb create app/views/employers/show.html.erb create app/views/employers/new.html.erb create app/views/employers/edit.html.erb create app/views/layouts/employers.html.erb create public/stylesheets/scaffold.css create app/controllers/employers_controller.rb create test/functional/employers_controller_test.rb create app/helpers/employers_helper.rb create test/unit/helpers/employers_helper_test.rb route map.resources :employers dependency model exists app/models/ exists test/unit/ exists test/fixtures/ create app/models/employer.rb create test/unit/employer_test.rb create test/fixtures/employers.yml create db/migrate create db/migrate/20090501175821_create_employers.rb

Page 69: A Z Introduction To Ruby On Rails

app/models/employer.rbdb/migrate/20090...1_create_employers.rbapp/views/employers/index.html.erbapp/views/employers/show.html.erbapp/views/employers/new.html.erbapp/views/employers/edit.html.erbapp/views/layouts/employers.html.erbpublic/stylesheets/scaffold.cssapp/controllers/employers_controller.rbapp/helpers/employers_helper.rbtest/functional/employers_controller_test.rbtest/unit/helpers/employers_helper_test.rbtest/unit/employer_test.rbtest/fixtures/employers.ymlroute map.resources :employers

Page 70: A Z Introduction To Ruby On Rails

class CreateEmployers < ActiveRecord::Migration def self.up create_table :employers do |t| t.string :name t.string :address t.string :city t.string :state t.string :zipcode t.timestamps end end

def self.down drop_table :employers endend

db/migrations/2009...create_employers.rb

Page 71: A Z Introduction To Ruby On Rails

rake db:migrate

Page 72: A Z Introduction To Ruby On Rails

map.root :controller => ʻemployersʼ

config/routes.rb

Page 73: A Z Introduction To Ruby On Rails

http://localhost:3000/

Page 74: A Z Introduction To Ruby On Rails

class Employer < ActiveRecord::Baseend

app/models/employer.rb

Page 75: A Z Introduction To Ruby On Rails

class Employer < ActiveRecord::Base validates_presence_of :name validates_length_of :city, :minimum => 3end

app/models/employer.rb

Page 76: A Z Introduction To Ruby On Rails

http://localhost:3000/employers/new

Page 77: A Z Introduction To Ruby On Rails
Page 78: A Z Introduction To Ruby On Rails

Controller => CRUD

Page 79: A Z Introduction To Ruby On Rails

Model => Logic

Page 80: A Z Introduction To Ruby On Rails

script/console

Page 81: A Z Introduction To Ruby On Rails
Page 82: A Z Introduction To Ruby On Rails

# GET /employers# GET /employers.xmldef index @employers = Employer.all

respond_to do |format| format.html # index.html.erb format.xml { render :xml => @employers } endend

app/controllers/employers_controller.rb

Page 83: A Z Introduction To Ruby On Rails

# GET /employers# GET /employers.xmldef index @employers = Employer.all

respond_to do |format| format.html # index.html.erb format.xml { render :xml => @employers } endend

app/controllers/employers_controller.rb

Page 84: A Z Introduction To Ruby On Rails

# GET /employers# GET /employers.xmldef index @employers = Employer.all

respond_to do |format| format.html # index.html.erb format.xml { render :xml => @employers } endend

app/controllers/employers_controller.rb

Page 85: A Z Introduction To Ruby On Rails

app/views/employers/index.html.erb

Page 86: A Z Introduction To Ruby On Rails

<%=h employer.name %>

app/views/employers/index.html.erb

Page 87: A Z Introduction To Ruby On Rails

<%= link_to 'New employer', ... %>

app/views/employers/index.html.erb

Page 88: A Z Introduction To Ruby On Rails

edit_employer_path(employer)

app/views/employers/index.html.erb

Page 89: A Z Introduction To Ruby On Rails

app/views/layouts/employers.html.erb

Page 90: A Z Introduction To Ruby On Rails

# GET /employers/new# GET /employers/new.xmldef new @employer = Employer.new

respond_to do |format| format.html # new.html.erb format.xml { render :xml => @employer } endend

app/controllers/employers_controller.rb

Page 91: A Z Introduction To Ruby On Rails

app/views/employers/new.html.erb

Page 92: A Z Introduction To Ruby On Rails

# POST /employers# POST /employers.xmldef create @employer = Employer.new(params[:employer])

respond_to do |format| if @employer.save flash[:notice] = 'Employer was successfully created.' format.html { redirect_to(@employer) } format.xml { render :xml => @employer, :status => :created, :location => @employer } else format.html { render :action => "new" } format.xml { render :xml => @employer.errors, :status => :unprocessable_entity } end endend

app/controllers/employers_controller.rb

Page 93: A Z Introduction To Ruby On Rails

# GET /employers/1# GET /employers/1.xmldef show @employer = Employer.find(params[:id])

respond_to do |format| format.html # show.html.erb format.xml { render :xml => @employer } endend

app/controllers/employers_controller.rb

Page 94: A Z Introduction To Ruby On Rails

app/views/employers/show.html.erb

Page 95: A Z Introduction To Ruby On Rails

# GET /employers/1/editdef edit @employer = Employer.find(params[:id])end

app/controllers/employers_controller.rb

Page 96: A Z Introduction To Ruby On Rails

app/views/employers/edit.html.erb

Page 97: A Z Introduction To Ruby On Rails

# PUT /employers/1# PUT /employers/1.xmldef update @employer = Employer.find(params[:id])

respond_to do |format| if @employer.update_attributes(params[:employer]) flash[:notice] = 'Employer was successfully updated.' format.html { redirect_to(@employer) } format.xml { head :ok } else format.html { render :action => "edit" } format.xml { render :xml => @employer.errors, :status => :unprocessable_entity } end endend

app/controllers/employers_controller.rb

Page 98: A Z Introduction To Ruby On Rails

# DELETE /employers/1# DELETE /employers/1.xmldef destroy @employer = Employer.find(params[:id]) @employer.destroy

respond_to do |format| format.html { redirect_to(employers_url) } format.xml { head :ok } endend

app/controllers/employers_controller.rb

Page 99: A Z Introduction To Ruby On Rails

DonʼtRepeatYourself

Page 100: A Z Introduction To Ruby On Rails

Partials

Page 101: A Z Introduction To Ruby On Rails

_form.html.erb

app/views/employers/_form.html.erb

Page 102: A Z Introduction To Ruby On Rails

app/views/employers/_form.html.erb

Page 103: A Z Introduction To Ruby On Rails

<%= render :partial => 'form' %>

Page 104: A Z Introduction To Ruby On Rails

app/views/employers/new.html.erb

Page 105: A Z Introduction To Ruby On Rails

app/views/employers/edit.html.erb

Page 106: A Z Introduction To Ruby On Rails

<%= render :partial => 'shared/form' %>

Page 107: A Z Introduction To Ruby On Rails

@employer = Employer.find(params[:id])

app/controllers/employers_controller.erb

Page 108: A Z Introduction To Ruby On Rails

before_filter :find_employer, :only => [:show, :edit, :update, :destroy]

app/controllers/employers_controller.rb

Page 109: A Z Introduction To Ruby On Rails

app/controllers/employers_controller.rb

Page 110: A Z Introduction To Ruby On Rails

private def find_employer @employer = Employer.find(params[:id]) end

app/controllers/employers_controller.rb

Page 111: A Z Introduction To Ruby On Rails

app/controllers/employers_controller.rb

Page 112: A Z Introduction To Ruby On Rails

script/generate scaffold Job name:string description:text

Page 113: A Z Introduction To Ruby On Rails

class CreateJobs < ActiveRecord::Migration def self.up create_table :jobs do |t| t.integer :employer_id t.string :name t.text :description t.timestamps end end

def self.down drop_table :jobs endend

db/migrations/2009...create_jobs.rb

Page 114: A Z Introduction To Ruby On Rails

rake db:migrate

Page 115: A Z Introduction To Ruby On Rails

belongs_tohas_onehas_manyhas_many :throughhas_one :throughhas_and_belongs_to_many

Page 116: A Z Introduction To Ruby On Rails

employersemployersModel: EmployerModel: Employerhas_many :jobshas_many :jobs

id integer

name string

address string

jobsjobsModel: JobModel: Jobbelongs_to :employerbelongs_to :employer

id integer

employer_id integer

name string

Page 117: A Z Introduction To Ruby On Rails

employersemployersModel: EmployerModel: Employerhas_one :jobhas_one :job

id integer

name string

address string

jobsjobsModel: JobModel: Jobbelongs_to :employerbelongs_to :employer

id integer

employer_id integer

name string

Page 118: A Z Introduction To Ruby On Rails

employersemployersModel: EmployerModel: Employerhas_many :jobshas_many :jobs

id integer

name string

address string

jobsjobsModel: JobModel: Jobbelongs_to :employerbelongs_to :employer

id integer

employer_id integer

name string

Page 119: A Z Introduction To Ruby On Rails

physiciansphysiciansModel: PhysicianModel: Physicianhas_many :appointmentshas_many :patients, :through => :appointments

has_many :appointmentshas_many :patients, :through => :appointments

id integer

name string

appointmentsappointmentsModel: AppointmentModel: Appointmentbelongs_to :physicianbelongs_to :patientbelongs_to :physicianbelongs_to :patient

id integerphysician_id integer

patient_id integer

patientspatientsModel: PatientModel: Patienthas_many :appointmentshas_many :physicians, :through => :appointments

has_many :appointmentshas_many :physicians, :through => :appointments

id integer

name string

Page 120: A Z Introduction To Ruby On Rails

physiciansphysiciansModel: PhysicianModel: Physicianhas_and_belongs_to_many :patientshas_and_belongs_to_many :patients

id integer

name stringphysicians_patientsphysicians_patients

physician_id integer

patient_id integerpatientspatients

Model: PatientModel: Patienthas_and_belongs_to_many :physicianshas_and_belongs_to_many :physicians

id integer

name string

Page 121: A Z Introduction To Ruby On Rails

class Job < ActiveRecord::Baseend

app/models/job.rb

Page 122: A Z Introduction To Ruby On Rails

class Job < ActiveRecord::Base belongs_to :employer

validates_presence_of :name validates_presence_of :descriptionend

app/models/job.rb

Page 123: A Z Introduction To Ruby On Rails

class Job < ActiveRecord::Base belongs_to :employer

validates_presence_of :name validates_presence_of :descriptionend

app/models/job.rb

Page 124: A Z Introduction To Ruby On Rails

class Employer < ActiveRecord::Base has_many :jobs

validates_presence_of :name validates_length_of :city, :minimum => 3end

app/models/employer.rb

Page 125: A Z Introduction To Ruby On Rails

map.resources :employers

app/controllers/employers_controller.rb

map.resources :employers, :has_many => :jobs

map.resources :jobs

Page 126: A Z Introduction To Ruby On Rails
Page 127: A Z Introduction To Ruby On Rails

before_filter :find_employer

app/controllers/jobs_controller.rb

Page 128: A Z Introduction To Ruby On Rails

app/controllers/jobs_controller.rb

Page 129: A Z Introduction To Ruby On Rails

private def find_employer @employer = Employer.find(params[:employer_id]) end

app/controllers/jobs_controller.rb

Page 130: A Z Introduction To Ruby On Rails

app/controllers/jobs_controller.rb

Page 131: A Z Introduction To Ruby On Rails

# GET /jobs# GET /jobs.xmldef index @jobs = @employer.jobs

respond_to do |format| format.html # index.html.erb format.xml { render :xml => @jobs } endend

app/controllers/jobs_controller.rb

Page 132: A Z Introduction To Ruby On Rails
Page 133: A Z Introduction To Ruby On Rails

# GET /jobs/new# GET /jobs/new.xmldef new @job = Job.new @job = @employer.jobs.build

respond_to do |format| format.html # new.html.erb format.xml { render :xml => @job } endend

app/controllers/jobs_controller.rb

Page 134: A Z Introduction To Ruby On Rails

@job = @employer.jobs.build

Page 135: A Z Introduction To Ruby On Rails

app/views/jobs/index.html.erb

Page 136: A Z Introduction To Ruby On Rails

# POST /jobs# POST /jobs.xmldef create @employer = Employer.find(params[:employer_id]) @job = @employer.jobs.build(params[:job])

respond_to do |format| if @job.save flash[:notice] = 'Job was successfully created.' format.html { redirect_to employer_job_url(@employer, @job) } format.xml { render :xml => @job, :status => :created, :location => @job } else format.html { render :action => "new" } format.xml { render :xml => @job.errors, :status => :unprocessable_entity } end endend

app/controllers/jobs_controller.rb

Page 137: A Z Introduction To Ruby On Rails

# GET /jobs/1# GET /jobs/1.xmldef show @job = @employer.jobs.find(params[:id])

respond_to do |format| format.html # show.html.erb format.xml { render :xml => @job } endend

app/controllers/jobs_controller.rb

Page 138: A Z Introduction To Ruby On Rails

app/views/jobs/show.html.erb

Page 139: A Z Introduction To Ruby On Rails

# GET /jobs/1/editdef edit @job = @employer.jobs.find(params[:id])end

app/controllers/jobs_controller.rb

Page 140: A Z Introduction To Ruby On Rails

app/views/jobs/edit.html.erb

Page 141: A Z Introduction To Ruby On Rails

# PUT /jobs/1# PUT /jobs/1.xmldef update @job = Job.find(params[:id])

respond_to do |format| if @job.update_attributes(params[:job]) flash[:notice] = 'Job was successfully updated.' format.html { redirect_to employer_job_url(@employer, @job) } format.xml { head :ok } else format.html { render :action => "edit" } format.xml { render :xml => @job.errors, :status => :unprocessable_entity } end endend

app/controllers/jobs_controller.rb

Page 142: A Z Introduction To Ruby On Rails

# DELETE /jobs/1# DELETE /jobs/1.xmldef destroy @job = Job.find(params[:id]) @job.destroy

respond_to do |format| format.html { redirect_to employer_jobs_url(@employer) } format.xml { head :ok } endend

app/controllers/jobs_controller.rb

Page 143: A Z Introduction To Ruby On Rails

app/views/employers/show.html.erb

Page 144: A Z Introduction To Ruby On Rails

app/views/jobs/index.html.erb

Page 145: A Z Introduction To Ruby On Rails

app/views/jobs/index.html.erb

Page 146: A Z Introduction To Ruby On Rails

app/views/employers/index.html.erb

Page 147: A Z Introduction To Ruby On Rails

app/views/employers/index.html.erb

Page 148: A Z Introduction To Ruby On Rails

app/controllers/employers_controller.rb

Page 149: A Z Introduction To Ruby On Rails

app/controllers/employers_controller.rb

Page 150: A Z Introduction To Ruby On Rails

app/views/employers/index.html.erb

Page 151: A Z Introduction To Ruby On Rails

app/views/employers/index.html.erb

Page 152: A Z Introduction To Ruby On Rails

app/controllers/jobs_controller.rb

Page 153: A Z Introduction To Ruby On Rails

app/controllers/employers_controller.rb

Page 154: A Z Introduction To Ruby On Rails

app/views/employers/index.html.erb

Page 155: A Z Introduction To Ruby On Rails

Next Steps

Page 156: A Z Introduction To Ruby On Rails

DRY up our Job views

Add search to our jobs

Add a logins for employers

Add tags to our jobs

Page 157: A Z Introduction To Ruby On Rails

Resources

Page 158: A Z Introduction To Ruby On Rails

Rails Guides

Agile Web Development (PP)

Intro to Ruby 1.9

Cucumber + RSpec

Page 159: A Z Introduction To Ruby On Rails

Contest!

Page 160: A Z Introduction To Ruby On Rails

Letʼs Chat