Rubyon Rails

22
Ruby on Rails The most awesome thing to hit the internet since Rick Astley UW – Eau Claire Web-Centric Programming May 30, 2009 Presenter: Kevin Gisi [email protected] Twitter: gisikw

Transcript of Rubyon Rails

Page 1: Rubyon Rails

Ruby on RailsThe most awesome thing to hit

the internet since Rick Astley

UW – Eau Claire Web-Centric Programming

May 30, 2009

Presenter: Kevin Gisi

[email protected]

Twitter: gisikw

Page 2: Rubyon Rails

About the Presenter

• Computer Engineering

• LTS – Web Development

• Independent Contractor

• Co-founder of the Eau Claire Ruby User Group

• Winner of the RailsRumble “Most Innovative” award

• Pragmatic Programmers Author

Page 3: Rubyon Rails

What is Ruby?

• Developed in the 1990’s in Japan

• C-based interpreted language

• Dynamically (“duck”) typed

• Based on Perl and Smalltalk

• Many different implementations (YARV, JRuby, Rubinius, IronRuby, MacRuby)

• Completely open-source

Page 4: Rubyon Rails

What is Rails?

• Web application framework

• Extracted from 37Signals in 2004

• Model – View – Controller

• Convention over Configuration

• Don’t Repeat Yourself (DRY)

• Representational State Transfer (REST)

• Completely open-source

Page 5: Rubyon Rails

Convention: REST

• Everything is a resource

• You Create (POST), Read (GET), Update (PUT), and Destroy (DELETE) resources

• Also need New, and Edit pages

Page 6: Rubyon Rails

Convention: URLs

• URLs should be pretty!

• Index /listings

• New /listings/new

• Show /listings/5

• Edit /listings/5/edit

• Create /listings

• Update /listings/5

• Destroy /listings/5

Page 7: Rubyon Rails

Ruby on Rails Tools

• RubyGems – Package Manager

• Plugins – Adds functionality

• Generators – Writes code for you

• Console – Access runtime environment

Page 8: Rubyon Rails

Server Structure

Front-endApache/Nginx

Server – 5000Mongrel

Server – 5001Mongrel

Server – 5002Mongrel

Database

Page 9: Rubyon Rails

What Runs On Rails?

• Hulu

• Twitter

• 37 Signals

• Blinksale

• Many UWEC Applications

Page 10: Rubyon Rails

2-Minute Ruby• Everything is an object

• Anything other than false or nil is “true”

• Functions always return something

• Classes can be modified at runtime

• @instance_variables

• @@class_variables

• CONSTANTS

Page 11: Rubyon Rails

2-Minute RubyArrays [ ]

• Ordered, dynamically sized

• my_array << value

• my_array[4]

• my_array.pop

Page 12: Rubyon Rails

2-Minute RubyHashes { }

• Not ordered

• Key, value pairs

• Keys are traditionally :symbols

• Awesome for methods

Page 13: Rubyon Rails

2-Minute RubyMethods

def foo(params = {})

“You said” + params[:message]

end

Page 14: Rubyon Rails

2-Minute RubyMethods (continued)

• mutate!, boolean?, assignment=

def method_missing

“We don’t do that!”

end

Page 15: Rubyon Rails

2-Minute RubyClasses

class Awesome

attr_accessor :pie

end

a = Awesome.new

a.pie

Page 16: Rubyon Rails

2-Minute RubyBlocks

my_array.each do |value|

puts “It was #{value}”

end

Page 17: Rubyon Rails

2-Minute RubyPretend I Didn’t Show You This

eval(“foo = 5”)

Page 18: Rubyon Rails

3-Minute RailsModels

• Validations

• Business logic

• Other object methods

• Anything that doesn’t go somewhere else

Page 19: Rubyon Rails

3-Minute RailsControllers

• Before filters

• Public actions

• Keep them small!

Page 20: Rubyon Rails

3-Minute RailsViews

• Display

• Mix of HTML and ERb (Embedded Ruby)

• Avoid logic here!

Page 21: Rubyon Rails

3-Minute RailsHelpers

• Custom embedded Ruby:

def paypal

“<form>Paypal donate code</form>”

end

Page 22: Rubyon Rails

Boo Slides!Let’s be awesome code

monkeys instead!