Slide 1

Slide 1 text

THE SUPER DUPER BASICS OF RAILS TOMMY PALMER @tommypalm

Slide 2

Slide 2 text

A bit of backstory...

Slide 3

Slide 3 text

I’m Tommy

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

EX-IMDER

Slide 6

Slide 6 text

Interactive Multimedia Design

Slide 7

Slide 7 text

Design

Slide 8

Slide 8 text

HTML, CSS + Wordpress

Slide 9

Slide 9 text

Aren’t you a developer?

Slide 10

Slide 10 text

HELL YEAH I AM!

Slide 11

Slide 11 text

What happened?

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Ruby on Rails is optimised for programmer happiness and productivity.

Slide 15

Slide 15 text

STUFF THAT I WISH I KNEW ABOUT RAILS TWO YEARS AGO & WHY I WANTED TO LEARN IT

Slide 16

Slide 16 text

Ruby is easy to read

Slide 17

Slide 17 text

Easier than this

Slide 18

Slide 18 text

question = "Isn't Ruby great?" question = question.upcase puts question => "ISN'T RUBY GREAT?"

Slide 19

Slide 19 text

The Belfast Ruby Scale

Slide 20

Slide 20 text

The Belfast Ruby Scale

Slide 21

Slide 21 text

Ruby is a language Rails is a framework

Slide 22

Slide 22 text

Rails lets you do common web app things • working with databases • rendering HTML • sending email • and more...

Slide 23

Slide 23 text

Rails comes packaged as a gem

Slide 24

Slide 24 text

EH?

Slide 25

Slide 25 text

Gems give extra functionality to Ruby: • working with apis • tools for blogging • stylesheets (sass)

Slide 26

Slide 26 text

Rails is a framework for rapid web development

Slide 27

Slide 27 text

ActiveRecord is for databases

Slide 28

Slide 28 text

ActionView renders our HTML

Slide 29

Slide 29 text

ActionMailer deals with emails

Slide 30

Slide 30 text

ActionPack deals with HTTP requests & responses

Slide 31

Slide 31 text

ActionModel deals with our data

Slide 32

Slide 32 text

ActionResource deals with HTTP actions (GET, PUT, POST, DELETE)

Slide 33

Slide 33 text

ActiveSupport extends Ruby with lots of helpful bits

Slide 34

Slide 34 text

Railties brings them all together

Slide 35

Slide 35 text

We also get the rails command

Slide 36

Slide 36 text

Generates a blank Rails app rails new ourcoolapp

Slide 37

Slide 37 text

Starts a local server rails server

Slide 38

Slide 38 text

No MAMP! Yay!

Slide 39

Slide 39 text

Starts our Rails app in the Terminal and interacts with it rails console

Slide 40

Slide 40 text

Before we continue

Slide 41

Slide 41 text

Does not generate a blog rails new blog

Slide 42

Slide 42 text

Does not generate Twitter rails new twitter

Slide 43

Slide 43 text

Does not generate Basecamp rails new basecamp

Slide 44

Slide 44 text

This message brought to you by @shylands

Slide 45

Slide 45 text

DESIGNERS! DON’T BE AFRAID OF...

Slide 46

Slide 46 text

THE TERMINAL It’s not that bad

Slide 47

Slide 47 text

The Terminal is where you are in your filesystem

Slide 48

Slide 48 text

Moving into your Rails app let’s us do the command line stuff cd ourcoolapp

Slide 49

Slide 49 text

A blank Rails app

Slide 50

Slide 50 text

/app is where most of our work happens

Slide 51

Slide 51 text

NOW FOR THE JUICY BITS!

Slide 52

Slide 52 text

Rails is an MVC framework

Slide 53

Slide 53 text

/models - Models /views - Views /controllers - Controllers

Slide 54

Slide 54 text

Models are our data types posts, users, photos, etc

Slide 55

Slide 55 text

Views are the HTML (with some Ruby magic)

Slide 56

Slide 56 text

Controllers link the Models and Views

Slide 57

Slide 57 text

routes.rb is our URLs

Slide 58

Slide 58 text

Views have the extension .html.erb

Slide 59

Slide 59 text

ERB stands for Embedded Ruby

Slide 60

Slide 60 text

<% @posts.each do |post| %>

<%= post.title %>

<%= post.body %>

<%= post.pub_date %>
<% end %>

Slide 61

Slide 61 text

Inside <% %> is evaluated Ruby code

Slide 62

Slide 62 text

Inside <%= %> is output to the HTML

Slide 63

Slide 63 text

Controllers process traffic and fill the ruby code

Slide 64

Slide 64 text

/posts/1 show.html.erb Post with ID 1 def show @post = Post.find(params[:id]) end

Slide 65

Slide 65 text

Model View Controller routes.rb Browser

Slide 66

Slide 66 text

Model View Controller routes.rb Browser

Slide 67

Slide 67 text

Model View Controller routes.rb Browser

Slide 68

Slide 68 text

Model View Controller routes.rb Browser Database

Slide 69

Slide 69 text

Model View Controller routes.rb Browser Database

Slide 70

Slide 70 text

Model View Controller routes.rb Browser Database

Slide 71

Slide 71 text

Model View Controller routes.rb Browser Database

Slide 72

Slide 72 text

Model View Controller routes.rb Browser Database

Slide 73

Slide 73 text

This generates our Model, Views, Controller and some other stuff... rails g scaffold post title:string body:text pub_date:date

Slide 74

Slide 74 text

...including Migrations!

Slide 75

Slide 75 text

Migrations are great!

Slide 76

Slide 76 text

Migrations let us incrementally update our database

Slide 77

Slide 77 text

class CreatePosts < ActiveRecord::Migration def change create_table :posts do |t| t.string :title t.text :body t.date :pub_date t.timestamps end end end

Slide 78

Slide 78 text

Migrates the database rake db:migrate

Slide 79

Slide 79 text

rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false). rake db:migrate:status # Display status of migrations rake db:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n). rake db:schema:dump # Create a db/schema.rb file that can be portably used against any DB supported by AR rake db:schema:load # Load a schema.rb file into the database rake db:seed # Load the seed data from db/seeds.rb rake db:setup # Create the database, load the schema, and initialize with the seed data More rake commands

Slide 80

Slide 80 text

Rails knows plurals of lots of objects (through ActiveSupport)

Slide 81

Slide 81 text

post - posts user - users octopus - octopi sheep - sheep

Slide 82

Slide 82 text

Model - post.rb Controller - posts_controller.rb Views - show, index, new & edit.html.erb Migration - (unix timestamp)_create_posts.rb routes.rb - resources :posts rails g scaffold post

Slide 83

Slide 83 text

Scaffolds let us prototype quickly

Slide 84

Slide 84 text

rails g scaffold post title:string body:text pub_date:date

Slide 85

Slide 85 text

Not so great in the long run

Slide 86

Slide 86 text

Generate separately rails g controller users rails g model user rails g migration CreateUsers

Slide 87

Slide 87 text

More useful code Less crappy code

Slide 88

Slide 88 text

Better use of time Cleaner application

Slide 89

Slide 89 text

Rails makes good web development easier

Slide 90

Slide 90 text

Being a Rails dev

Slide 91

Slide 91 text

New to Rails?

Slide 92

Slide 92 text

Used it before?

Slide 93

Slide 93 text

Rails is pretty sweet

Slide 94

Slide 94 text

thanks! TOMMY PALMER @tommypalm [email protected] [email protected]

Slide 95

Slide 95 text

questions to @ belfastruby TOMMY PALMER @tommypalm [email protected] [email protected]