Beginners' guide to Ruby on Rails

24

Transcript of Beginners' guide to Ruby on Rails

Page 1: Beginners' guide to Ruby on Rails
Page 2: Beginners' guide to Ruby on Rails

What we’ll talk about

What is Ruby on Rails?

A brief history

MVC architecture in a nutshell

Distinctive framework features

Why Ruby? Why Rails?

Demo

Page 3: Beginners' guide to Ruby on Rails

What is RoR?

Open Source

Abstraction providing generic server-side

functionality

Used for web application development

It’s a framework for the Ruby language

..so don’t confuse it with plain Ruby!

Page 4: Beginners' guide to Ruby on Rails

A brief history

Originates in David Heinemeier Hansson’s work

First released in July 2004

Apple ships RoR with Mac OS X Leopard since

2007

Major new developments with v2.3 in 2009

Templates

Generate skeleton applications

Custom gems and configurations

Latest: v3.0.5, 27 February 2011

Page 5: Beginners' guide to Ruby on Rails

MVC architecture

RoR is based on the Model-View-Controller design

It’s an architectural development pattern

Widely used in many other frameworks:

Oracle Application Framework, Cocoon, JSF (Java)

ASP.NET (C#)

SproutCore, JavascriptMVC (Javascript)

Django, Pylons (Python)

CakePHP (PHP)

PureMVC (many languages)

Page 6: Beginners' guide to Ruby on Rails
Page 7: Beginners' guide to Ruby on Rails

MVC on Rails

Models are Ruby classes, used to store and validate data

Handles migrations

They talk to databases

MySQL

SQLite

PostgreSQL

NoSQL

MongoDB

Cassandra

“Chubby guy in the back room” crunching the numbers

Page 8: Beginners' guide to Ruby on Rails

Model snippet

class Person < ActiveRecord::Basevalidates_presence_of :namehas_many :wifes

end

Person.create(:name => ”Muhammad Ali”).valid? # => truePerson.create(:name => nil).valid? # => false

Page 9: Beginners' guide to Ruby on Rails

Migration snippet

class AddReceiveNewsletterToUsers < ActiveRecord::Migrationdef self.upchange_table :users do |t|t.boolean :receive_newsletter, :default => false

endUser.update_all ["receive_newsletter = ?", true]

end

def self.downremove_column :users, :receive_newsletter

endend

Page 10: Beginners' guide to Ruby on Rails

MVC on Rails

What the user sees

HTML, CSS, XML, Javascript (jQuery)

JSON, RSS, Atom

Automatically generated “view-puppets”

Visual representation of data

Does not have direct access to the model!

It shouldn’t do lots of processing or calculation

Page 11: Beginners' guide to Ruby on Rails

View snippet

<!-- app/views/items/new.rhtml -->

<%= form_tag :action => “create” %>Name: <%= text_field “item”, “name”%><br/>Email: <%= text_field “item”, ”email”%><br/>Password: <%= hidden_field “item”, “password”%><br/><%= submit_tag %><%= end_form_tag %>

Page 12: Beginners' guide to Ruby on Rails

MVC on Rails

Gateway between the model and the view

Handles user requests

Does parsing and data submissions

Takes care of sessions, cookies

Works out what data to show and what views to render

”The best controller: it gives orders without knowing (or

caring) how it gets done”

Page 13: Beginners' guide to Ruby on Rails

Controller snippet

class ItemsController < ApplicationControllerdef edit@item = Item.find(params[:id])

if request.post? @item.update_attributes(params[:item])redirect_to :action => 'edit', :id => @item.id

endend

end

Page 14: Beginners' guide to Ruby on Rails

MVC on Rails

To summarize:

1. Keep your view code skinny

2. Keep your controller code skinny

3. The fatness is in the model

Page 15: Beginners' guide to Ruby on Rails

Framework features

Code generators

Awesome built-in functions => rapid development

Tons of “gems” to choose from

Cross-platform compatibility

Automated operation (CRUD)

Create, Retrieve, Automate and Delete

Simplified testing (Rake script)

Page 16: Beginners' guide to Ruby on Rails

Code generator snippet

#model generator (general)

ruby script/generate model model_name (v2.3)

rails generate model model_name (v3)

#model generator (example)

rails generate model user name:string hashed_password:string

#controller generator (general)

ruby script/generate controller controller_name method_name(s) (v2.3)

rails generate controller controller_name method_name(s) (v3)

#controller generator (example)

rails generate controller store index

Page 17: Beginners' guide to Ruby on Rails

RoR servers

Mongrel

Webbrick

Thin

Apache (mod_passanger)

..so it’s very scalable!

Page 18: Beginners' guide to Ruby on Rails

Famous projects on Rails

Twitter (microblogging)

Crunchbase (companies database)

BaseCamp (project management)

Hulu (online tv)

Yellowpages.com (phone database)

Xing (business network)

Page 19: Beginners' guide to Ruby on Rails

Why Ruby?

Interpreted language => greater flexibility

Provides JIT (just in time compilation)

Garbage collection

Able to generate code on the fly

Cleaner syntax (no more “Verbose verbose is too verbose for verbose”)

Many implementations:

Jruby, IronRuby, Rubinius (Ruby & C++), MacRuby (ObjC)

Page 20: Beginners' guide to Ruby on Rails

Why Rails?

Based on Ruby

Easy to implement CGI scripts

Rapid web-application development

Designed to make programming work easier

Less coding, more functionality

Thousands of plugins

Don’t reinvent the wheel

Page 21: Beginners' guide to Ruby on Rails
Page 22: Beginners' guide to Ruby on Rails

Creating a blog with

Page 23: Beginners' guide to Ruby on Rails
Page 24: Beginners' guide to Ruby on Rails