PDF Ruby on Rails 3 Day BC

64
Ruby on Rails 3-Day BootCamp 1

description

PDF version of the boot camp slides

Transcript of PDF Ruby on Rails 3 Day BC

Page 1: PDF Ruby on Rails 3 Day BC

Ruby on Rails3-Day BootCamp

1

Page 2: PDF Ruby on Rails 3 Day BC

Introductions

Name

Coding Experience

What would you like to learn?

2

Page 3: PDF Ruby on Rails 3 Day BC

Curriculum

RoR Toolset

Rails Walk-Through

Gems and Deployment

Full Apps and Ruby inside Rails

3

Page 4: PDF Ruby on Rails 3 Day BC

ScheduleTimes Day 1 Day 2 Day3

9am - 10:30 Tools RubyToolBox BootStrap Break

10:45 - 12 Scaffold and Views Cucumber RailsApps and

Hobo / SpreeLunch

1pm - 2:30 Routes and Controllers Devise Calculator

Break

2:45 - 4:30 Models and Testing I18N and Haml Translator

4

Page 5: PDF Ruby on Rails 3 Day BC

Learning Goals

Basic Rails

Common Patterns and Practices

Where to go for help

5

Page 6: PDF Ruby on Rails 3 Day BC

Learning Path

Experimentation

Test First Teaching

Ask Questions

Resources for finding answers

6

Page 7: PDF Ruby on Rails 3 Day BC

Structure

Explanation (me talking)

Demonstration (me typing)

Experimentation (in-class coding)

Questions

7

Page 8: PDF Ruby on Rails 3 Day BC

RoR ToolsRVM / Pik / Jewelry Box / Rb.Env

Git / Github / Heroku

Sublime / Vi / RubyMine

RubyGems / RubyToolbox

Testing / RSpec / Cucumber

Terminal / irb / Rails Console

8

Page 9: PDF Ruby on Rails 3 Day BC

RoR ResourcesRailsGuides - guides.rubyonrails.org

Rails API - api.rubyonrails.org

RubyDoc - ruby-doc.org

RailsCasts - railscasts.com

Github ReadMe

Ruby ToolBox - www.ruby-toolbox.com

9

•RailsGuides - guides.rubyonrails.org

•Rails API - api.rubyonrails.org

•RubyDoc - ruby-doc.org

•RailsCasts - railscasts.com

•Github ReadMe

•Ruby ToolBox - www.ruby-toolbox.com

Page 10: PDF Ruby on Rails 3 Day BC

Ruby vs. RailsRuby is the Language -

developed 1993

Yukihiro Matsumoto “Matz”

Rails is the Framework -

developed 2004

David Heinemeier Hansson “DHH”

10

Page 11: PDF Ruby on Rails 3 Day BC

The Ruby Way Written in C

Influenced by SmallTalk & Pearl

Ruby is for People

MINSWAN

OSS (Open Source Software)

RubyGems

11

Page 12: PDF Ruby on Rails 3 Day BC

The Rails Way

Opinionated Software

Convention over Configuration

DRY (Don’t Repeat Yourself)

TDD (Test Driven Development)

Less Code, More Productivity

12

Page 13: PDF Ruby on Rails 3 Day BC

ToolsCommand Line

Ruby / RubyGems

Rails

Source Control - Git

IDE

Deployment - Heroku

13

Page 14: PDF Ruby on Rails 3 Day BC

Command Line

Command Prompt (CLI)

Mac: Terminal, iTerm, etc...

Unix: bash, zsh, csh, etc....

Windows: Command Prompt w/ Ruby on Rails

14

Page 15: PDF Ruby on Rails 3 Day BC

Ruby

Ruby -v

1.9.2, 1.9.3, 2.0.0 (1.8.7)

gem list

Bundler, Rake, rubygems, etc...

15

Page 16: PDF Ruby on Rails 3 Day BC

RVMRuby Version Manager

rvm list

rvm use

rvm gemset list

rvm.io

16

Page 17: PDF Ruby on Rails 3 Day BC

Ruby VersionsMRI, YARV, Standard Library, C-Ruby

JRuby - Java

Rubinius - Pure Ruby

MagLev - SmallTalk

MacRuby - Objective-C

IronRuby - C# (for .Net)

17

Page 18: PDF Ruby on Rails 3 Day BC

Rails

Rails -v

2.0-3, 3.0-2, 4.0

18

Page 19: PDF Ruby on Rails 3 Day BC

RVM DemoGemset for Rails 4

rvm use --create 2.0.0@rails4

gem install rails --version 4.0.0.beta1

mkdir rvm_test

cd rvm_test

rvm --rvmrc --create

19

Page 20: PDF Ruby on Rails 3 Day BC

Git

which git

git --version

20

Page 21: PDF Ruby on Rails 3 Day BC

Git

Source Control System

Timeline, Track Changes

Branching and Merging

Code Safety

21

Page 22: PDF Ruby on Rails 3 Day BC

Git Commandsgit clone / init

git status

git add / rm

git commit

git push

git log

22

Page 23: PDF Ruby on Rails 3 Day BC

Git Remotes

GitHub - www.github.com

Heroku - www.heroku.com

23

Page 24: PDF Ruby on Rails 3 Day BC

Git Help

Git Site - git-scm.com

Git Immersion - gitimmersion.com

GitHub Help - help.github.com

24

Page 25: PDF Ruby on Rails 3 Day BC

Git Demomkdir git_test

cd git_test

git init

touch test_file.txt

git status

git add .

git commit -m “add a file”

git status

25

Page 26: PDF Ruby on Rails 3 Day BC

Git Democd ..

git clone git_test/.git clone_test

cd clone_test

ls

git remote -v

touch clone.txt

git commit -am “add another file”

git push

26

Page 27: PDF Ruby on Rails 3 Day BC

Editor

Sublime Text 2 / TextMate

RubyMine

KomodoEdit

Eclipse

Vi / Vim / Emacs

27

Page 28: PDF Ruby on Rails 3 Day BC

Sublime Text 2

Command line aliasWin: doskey subl="C:\Program Files\Sublime Text 2\sublime_text.exe" $*

Mac: ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" ~/bin/subl

Linux: sudo ln -s /opt/SublimeText2/sublime_text /usr/bin/subl

How to Open a folder

28

Command line aliasWin: doskey subl="C:\Program Files\Sublime Text 2\sublime_text.exe" $*Mac: ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" ~/bin/sublLinux: sudo ln -s /opt/SublimeText2/sublime_text /usr/bin/sublHow to Open a folder

Page 29: PDF Ruby on Rails 3 Day BC

BREAK!

10-15 minutes

29

Page 30: PDF Ruby on Rails 3 Day BC

Rails

New Rails App

Modify the HomePage

Scaffold

MVC

Layers

30

Page 31: PDF Ruby on Rails 3 Day BC

Create a New App

rails

Shows Options

rails new <app_name> --options

31

Page 32: PDF Ruby on Rails 3 Day BC

Create a Class Management App

We want an app to manage Students, Classes, and Teachers

rails new class_mngmt

32

rails new class_mngmt

Page 33: PDF Ruby on Rails 3 Day BC

The Application Directory

The Root of our App: Rails root

Entire App in one directory!

33

Page 34: PDF Ruby on Rails 3 Day BC

Run the App

cd class_mngmt

rails

see rails commands

rails server

34

rails server

Page 35: PDF Ruby on Rails 3 Day BC

Save a Baseline with git

git init

git add .

git commit -m “add a bare rails app to track changes”

35

git initgit add .git commit -m “add bare rails app to track”

Page 36: PDF Ruby on Rails 3 Day BC

Modify the HomePage

public/index.html

public folder contains static content

36

Page 37: PDF Ruby on Rails 3 Day BC

Scaffolding

rails generate

see the generators

rails g scaffold Student full_name age:integer bio:text title

37

rails g scaffold Student full_name age:integer bio:text title

Page 38: PDF Ruby on Rails 3 Day BC

MVC

BrowserRequest

Controller

Model

Database

View

Response

38

Page 39: PDF Ruby on Rails 3 Day BC

Shorten Bio Demo

Shorten the bio on the view page

Add a read more link

39

Page 40: PDF Ruby on Rails 3 Day BC

Helpers

Presentation Logic

Presenter Pattern

Ruby Module - Included on all views

Method Definitions

Help generate html

40

Page 41: PDF Ruby on Rails 3 Day BC

View Exercise

On the main Student page

Change the Listing Students to My Students or All Students

Show only the Full Name and title

Have the Full Name link to the view page

41

Page 42: PDF Ruby on Rails 3 Day BC

Lunch!

12pm - 1pm

42

Page 43: PDF Ruby on Rails 3 Day BC

Routing

rake routes

public folder

43

Page 44: PDF Ruby on Rails 3 Day BC

hello.html

create a hello file

find it in the browser

44

Page 45: PDF Ruby on Rails 3 Day BC

RESTful Routes

Representational State Transfer (REST)

guides.rubyonrails.org/routing.html

45

Page 46: PDF Ruby on Rails 3 Day BC

Controller Actions

index, show, new, edit, create, update, destroy

46

Page 47: PDF Ruby on Rails 3 Day BC

New Controller

rails g controller Welcome

47

Page 48: PDF Ruby on Rails 3 Day BC

Welcome

put hello.html in the welcome folder

app/views/welcome/hello.html

48

Page 49: PDF Ruby on Rails 3 Day BC

Find it!

http://localhost:3000/welcome/hello

49

Page 50: PDF Ruby on Rails 3 Day BC

Break!

10-15 minutes

50

Page 51: PDF Ruby on Rails 3 Day BC

Active Record

Ruby Driven Schema

Where are the methods?

51

Page 52: PDF Ruby on Rails 3 Day BC

Students Manage Their Courses

As a student we want to add our courses.

52

Page 53: PDF Ruby on Rails 3 Day BC

Add Courses

rails g Course title description:text student_id:integer

53

Page 54: PDF Ruby on Rails 3 Day BC

Relationships

Student has many courses

A Course belongs_to a Student

54

Page 55: PDF Ruby on Rails 3 Day BC

3 Kinds of Relationships

one-to-one

one-to-many

many-to-many

55

Page 56: PDF Ruby on Rails 3 Day BC

Rails DB / Console

rails db

login with the db credentials

rails console

irb with your app loaded

56

Page 57: PDF Ruby on Rails 3 Day BC

Add email to Student

rails g migration AddEmailToStudents email

57

Page 58: PDF Ruby on Rails 3 Day BC

Test Driven Rails

add rspec

test drive our business rules

58

Page 59: PDF Ruby on Rails 3 Day BC

Validations & callbacks

guides.rubyonrails.org/active_record_validations_callbacks.html

Validate a Student has a name

59

Page 60: PDF Ruby on Rails 3 Day BC

Validate the student email

presence and format

60

Page 62: PDF Ruby on Rails 3 Day BC

Environments

development

test

production

62

Page 63: PDF Ruby on Rails 3 Day BC

Rails App Walk Through

What is all this stuff?!

63

Page 64: PDF Ruby on Rails 3 Day BC

Questions?

??

64