22. Why Rails
Ruby is descriptive clean language
Latest concepts
Migrations
BrisbaneRails.com
23. What’s Rails
Rails is built on Ruby
Rails is a framework
Rails adds lots of DSLs and extra methods to
Ruby
Rails uses some of the advanced features of
Ruby to hide the complicated stuff
BrisbaneRails.com
24. Community
Ruby and Rails community is strong &
supportive!
Approx. 20 conferences a year
US, Ireland, Japan, Singapore, ......
Very Active IRC
Very active development
BrisbaneRails.com
26. RailsCamp
Bi-Annual event in Australia
Now in NZ, Europe and US
Starts Friday afternoon
What happens at Railscamp stays.......
End Monday morning
BrisbaneRails.com
31. Work Opportunities
Ruby on Rails developers are in high demand
$60K - $120+K (more overseas)
$600 - $1200/day freelance
Melbourne & Sydney highest demand
BrisbaneRails.com
34. Ruby Perception (Actual)
Very supportive community
Very opinionated community
Very sharing (gems/code/blogs)
Very quality conscious
Majority devs do TDD/BDD
BrisbaneRails.com
37. RVM
Ruby Version Manager
Allows multiple implementations
Allows multiple versions of implementations
Easy swap around and gem management
BrisbaneRails.com
38. Server
BEFORE
fast-cgi with reverse proxies
NOW
Passenger (mod rails)
heroku.com
BrisbaneRails.com
39. Real World Web Apps
American Yellow Pages
Twitter
Hulu
Slideshare
github
Redbubble
IPHONE APIS
BrisbaneRails.com
41. Gems
RubyGems is a package manager for the
Ruby programming language that provides a
standard format for distributing Ruby
programs and libraries (in a self-contained
format called a "gem"), a tool designed to
easily manage the installation of gems, and a
server for distributing them.
BrisbaneRails.com
48. No Type Casting Ruby
No Type declarations
value = 1
puts value -> 1
value = “hello”
puts value -> “hello”
BrisbaneRails.com
49. Everything is an object Ruby
puts 99.class -> Fixnum
puts “hello”.class -> String
puts nil.class -> NilClass
BrisbaneRails.com
50. Skip parenthesis Ruby
Most of the time
Easier to read puts “hello”
DSL friendly puts(“hello”)
BrisbaneRails.com
51. Variables Ruby
Constant
Local VAR = “constant”
Instance var = “local”
@var = “instance”
Class @@var = “class”
$var = “smelly”
Global
BrisbaneRails.com
52. Naming Conventions Ruby
underscore separated - variables and methods
full_name
All Caps - constants
SERVER_ID
CamelCase for classes
ActiveRecord
BrisbaneRails.com
57. Symbols Ruby
Identifier
Not assignable {:password => “secret”}
Mainly used in
hashes :password = 42 (invalid)
var = :password
puts var -> :password
BrisbaneRails.com
58. true, false, nil Ruby
good = true
bad = false
unknown = nil
BrisbaneRails.com
59. falsies Ruby
nil and false
everything else is true
var = 0
0 => true if var
puts “hello”
“0” => true
end
“” => true -> “hello”
BrisbaneRails.com
60. Assignment Ruby
value = “hello”
a = 10
a += 1 => 11
b ||= 10 => 10
b ||= 500 => 10
BrisbaneRails.com
61. Conditions Ruby
If statement
if name == “Fred”
Unless (invert) put “Hello, Fred”
end
var = false
if var
puts “never seen”
end
unless var
puts “always seen”
end BrisbaneRails.com
62. Conditions Ruby
Single line conditions
var = true
puts “shown” if var
puts “hide” unless var
puts “I win” if score > 11
BrisbaneRails.com
63. Methods Ruby
def say_hello
puts “hello”
end
say_hello -> “hello”
def my_name_is name
“my name is “ + name
end
puts my_name_is “Fred” -> “my name is Fred”
BrisbaneRails.com
64. Classes Ruby
Class MyThing
def initialize name
@name = name
end
attr_accessor :name, :age
def something
name + “ is “ + age + “years old”
end
end
a = MyThing.new “Fred”
puts a.name
a.age = 42
puts a.something -> “Fred is 42 years old”
BrisbaneRails.com
65. Iterator and blocks Ruby
5.times do
puts “Nice for-loop, eh?”
end
5.times { puts “One-liner block form” }
(1..3).each do |n|
puts “Say hello to number #{n}!”
end
[1,2,3].map { |n| n * 2 } -> [2, 4, 6]
BrisbaneRails.com
73. Testing Ruby
require 'test/unit'
def my_name_is name
“my name is “ + name
end
class TestIt < Test::Unit::TestCase
def test_it
assert_equal "my name is Fred",
my_name_is “Fred"
end
end
BrisbaneRails.com
74. Testing - RSpec Ruby
require 'rspec'
def greeting name
“my name is “ + name
end
describe Greeting
it “should return message with name” do
greeting.should == "my name is Fred”
end
end
BrisbaneRails.com
80. Ruby on Rails
Convention over configuration
DRY - Don’t Repeat Yourself
MVC - Model View Controller
BrisbaneRails.com
81. Ruby on Rails
Convention over configuration
DRY - Don’t Repeat Yourself
MVC - Model View Controller
ORM - Object Relation Mapping
BrisbaneRails.com
82. Ruby on Rails
Convention over configuration
DRY - Don’t Repeat Yourself
MVC - Model View Controller
ORM - Object Relation Mapping
Migrations
BrisbaneRails.com
83. Ruby on Rails
Convention over configuration
DRY - Don’t Repeat Yourself
MVC - Model View Controller
ORM - Object Relation Mapping
Migrations
Restful
BrisbaneRails.com
84. Convention over
Configuration
Consistent Structure
Naming conventions
Controller names -> Model names
Model names -> table names
BrisbaneRails.com
85. Don’t Repeat Yourself
DRY
One copy of a method
Re-usable methods
partial forms
BrisbaneRails.com
86. Model View Controller
MVC
1979 concept
Controller manages
action
Model connects to DB
Views separated from
logic
Router
BrisbaneRails.com
87. Controller Rails
class CompaniesController < ApplicationController
def index
@companies = Company.all
end
def show
@company = Company.find(params[:id])
end
def new
@company = Company.new
end
def create
@company = Company.new(params[:company])
if @company.save
redirect_to @company, :notice => "Successfully created company."
else
render :action => 'new'
end
end
BrisbaneRails.com
88. View - Index Rails
<table>
<tr>
<th>Name</th>
<th>Website</th>
</tr>
<% for company in @companies %>
<tr>
<td><%= company.name %></td>
<td><%= company.website %></td>
<td><%= link_to "Show", company %></td>
<td><%= link_to "Edit", edit_company_path(company) %></td>
<td><%= link_to "Destroy", company, :confirm => 'Are you .
</tr>
<% end %>
</table>
<p><%= link_to "New Company", new_company_path %></p>
BrisbaneRails.com
89. View - Index .haml Rails
%table
%tr
%th Name
%th Website
- for company in @companies
%tr
%td= company.name
%td= company.website
%td= link_to "Show", company
%td= link_to "Edit", edit_company_path(company)
%td= link_to "Destroy", company, :confirm => 'Are you ...
%p = link_to "New Company", new_company_path
BrisbaneRails.com
91. Model Rails
class Company < ActiveRecord::Base
attr_accessible :name, :website
has_many :contacts
end
BrisbaneRails.com
92. What’s ORM
Object-Relational mapping is a programming
technique for converting data between
incompatible type systems in databases and
object-oriented programming languages
Source: wikipedia
BrisbaneRails.com
93. Quick Example #1
select *
from companies
where id = 12
item = Company.find(12)
BrisbaneRails.com
94. Quick Example #2
select *
from companies
where name = “Logical”
item = Company.find_by_name(“Logical”)
OR
Item = Company.where(:name=>”Logical”)
BrisbaneRails.com
95. Quick Example #3
update companies
set name = “Logical Plus”
where id = 12
item=Company.find(12)
item.name = “Logical Plus”
item.save
OR
Company.find(12).update_attributes(:name=>”Logical
Plus”)
BrisbaneRails.com
96. Quick Example #4
insert into companies (name, postcode)
values (“Logical”, 4121)
item=Company.new
item.name = “Logical”
item.postcode = 4121
item.save
OR
Company.create(:name=>”Logical”, :postcode=>4121)
BrisbaneRails.com
97. Model
class Company < ActiveRecord::Base
has_many :contacts
has_many :invoices
end
class Contact < ActiveRecord::Base
belongs_to :company
has_many :invoices
end
class Invoice < ActiveRecord::Base
has_many :invoice_items
belongs_to :company
belongs_to :contact
end
class InvoiceItem < ActiveRecord::Base
belongs_to :invoice
end
BrisbaneRails.com
100. Rails Routes
companies_path GET /companies(.:format) :action=>"index”
company_path GET /companies/:id(.:format) :action=>"show"
new_company_path GET /companies/new(.:format) :action=>"new"
POST /companies(.:format) :action=>"create"
edit_company_path GET /companies/:id/edit(.:format) :action=>"edit"
PUT /companies/:id(.:format) :action=>"update"
DELETE /companies/:id(.:format) :action=>"destroy"
BrisbaneRails.com