Day 2 - Intro to Rails

11
Intro to Rails

Transcript of Day 2 - Intro to Rails

Page 1: Day 2 - Intro to Rails

Intro to Rails

Page 2: Day 2 - Intro to Rails

What do we look for in a framework?

• Please don’t suck– Rails does not suck

• Does it follow Model-View-Controller?– Yes – Since Rails 1 it’s been the standard bearer for how to do MVC on the web, copied in almost every language

• Does it help me avoid repeating myself (DRY)?– Yes

• Is it self documenting?– Yes, it has a set of rules that generally make most documentation unnecessary

• Is it flexible enough to bend to my application needs?– Yes

• Do other people use it?– Good gosh yes

• Will it work with my database?– Yes

• Is it still going to be around in X years?– Ruby has Rails– Python has Django– Groovy has Grails– C# has MVC– PHP has fragmented framework Hell (aka – who knows?)– Java has a few major players (Struts 2, Play, etc)

Page 3: Day 2 - Intro to Rails

Request Flow

Page 4: Day 2 - Intro to Rails

Rack

Watch this excellent walkthrough of Rack Middleware:

http://railscasts.com/episodes/151-rack-middleware

Summary: It’s a layer of ruby code that passes requests into your app and sends responses back out. You can add layers to do pre/post processing on all requests prior to beginning ANY of your application code.

Page 5: Day 2 - Intro to Rails

Before the Request

• Configuration

• Initializers

• Gems

• Environments

• Asset Pipeline

Page 6: Day 2 - Intro to Rails

Models / ActiveRecord

class Post < ActiveRecord::Base

belongs_to :category

has_many :tags, through: :posts_tags

validates :title, presence: true

before_save :create_slug, only: :create

scope :newest_first, order(‘created_at DESC’)

scope :active, where(‘active = ?’,true)

scope :newest_active, newest_first.active

scope :search, lambda do |text|

where(‘title LIKE ?’,”%#{text}%”)

end

def create_slug

self.slug = title.downcase.squish.sub(‘ ‘,’-’)

end

end

post = Post.new(title: ‘Some title’)

post.save!

OR

post = Post.create(title: ‘Some title’)

post.slug # some-title

post.id # 1

post.created_at # Created datetime

post.updated_at # Updated datetime

post.title = ‘New title’

post.save!

# Relations

post.tags.first

post.tags.count

post.category.name

post = Post.include(:tags) # Eager load

post =

Post.search(‘some’).newest_active.first

Page 7: Day 2 - Intro to Rails

Migrationsclass CreateInitialTables < ActiveRecord::Migration

def up

create_table :posts do |t|

t.string :title

t.text :body

t.string :slug

t.integer :category_id

t.timestamps

end

# … create more tables…

add_index :tags, [:name,:something], unique: true

execute “UPDATE posts SET field = ‘value’ WHERE stuff = ‘happens’”

end

def down

drop_table :posts

end

def change

add_column :posts, :user_id, :integer

end

end$ rake db:migrate

Page 8: Day 2 - Intro to Rails

Controllers and REST

Class PostsController < ApplicationController

before_filter :authenticate, only: :destroy

def index # GET /posts

end

def new # GET /posts/new

end

def create # POST /posts

end

def show # GET /posts/:id

end

def edit # GET /posts/:id/edit

end

def update # PUT /posts/:id

end

def destroy # DELETE /posts/:id

end

end

# Routes

resources :posts

OR limit it

resources :posts, only: [:create,:new]

Page 9: Day 2 - Intro to Rails

Views

/app/views

/layouts

/application.html.erb

/posts

/new.html.slim

/new.json.rabl

/index.xml.erb

/_widget.html.erb

# slim example

.post

h2=post.title

.body.grid-8=post.body

# erb example

<div class=“post”>

<h2><%=post.title%></h2>

<div class=“body grid-8”>

<%=post.body%>

</div>

</div>

Page 10: Day 2 - Intro to Rails

Generators

• rails new .

• rails g scaffold article name content:textpublished_on:date

Page 11: Day 2 - Intro to Rails

LET’S WRITE SOME CODE!