37
Ruby on Rails 3 Nam Ho December 2010, BarCamp Saigon

Ruby on Rails 3

Embed Size (px)

DESCRIPTION

My presentation at Barcamp Saigon 2010

Citation preview

Page 1: Ruby on Rails 3

Ruby on Rails 3

Nam Ho

December 2010, BarCamp Saigon

Page 2: Ruby on Rails 3

2

About me• Working at KMS Technology

• A member of ALT.NET Saigon group

@hotrannam

Page 3: Ruby on Rails 3

3

Ruby on Rails• Web framework• Ruby• Model-View-Controller pattern• Open source• Created by David H. Hansson• Maintained and developed by Rails core team

Page 4: Ruby on Rails 3

4

Ruby on Rails• Full-stack framework

– Gives everything you need to create a web app– Pretty much forces to use it

• Convention over configuration– Common structures and naming conventions– Don’t break conventions unless you really have to

• Don’t repeat yourself– Code is written in just one place– Easier for changes

• Testing

Page 5: Ruby on Rails 3

Confidential 5

Getting started

Page 6: Ruby on Rails 3

6

Starting a new apprails new barcamp

Page 7: Ruby on Rails 3

7

Starting a new apprails server

Page 8: Ruby on Rails 3

8

Starting a new app

Page 9: Ruby on Rails 3

9

Quick and easy?More information: http://rubyonrails.org/download

Page 10: Ruby on Rails 3

Confidential 10

Model

Page 11: Ruby on Rails 3

11

Model• Contains almost the application logic

– The heart of application

• Object Relational Mappings– Active Record– Data Mapper– Mongo Mapper

Page 12: Ruby on Rails 3

Active Recordrails g model Articlerake db:migraterails console

Table and Class name convention

The generated model class

The generated database migration file

Page 13: Ruby on Rails 3

Active Record: CRUDarticle = Article.newarticle.title = “Rails 3”article.body = “Barcamp Saigon”article.save

Create

Page 14: Ruby on Rails 3

Active Record: CRUDarticle = Article.firstarticle = Article.find(id)article = Article.find_by_title(“Rails 3”)articles = Artcile.all()

Lots of dynamic finders

Read

Page 15: Ruby on Rails 3

Active Record: CRUDarticle.title = “Web development with Rails 3”article.save

Update

Page 16: Ruby on Rails 3

Active Record: CRUDarticle.destroyArticle.delete(id)Article.delete_all(“created_at < '2011-01-01'")

Delete

Page 17: Ruby on Rails 3

Confidential 17

Active Record: Scope• A scope represents a narrowing of a database query

articles = Article.published.where_title(“Rails”)

Page 18: Ruby on Rails 3

Confidential 18

Active Record: Validationarticle.title = nilarticle.savearticle.errors.full_messages

Page 19: Ruby on Rails 3

Confidential 19

Active Record: Custom Val.comment = article.comments.create :body => “Great article!”comment.errors.full_messages

Page 20: Ruby on Rails 3

Confidential 20

Active Record: Relationship

1 : 1

Page 21: Ruby on Rails 3

Confidential 21

Active Record: Relationship

1 : n

Page 22: Ruby on Rails 3

Confidential 22

Active Record: Relationship

n : n

Page 23: Ruby on Rails 3

Active Record: Callbacks• before_create• after_create• before_save• after_save• before_destroy• after_destroy

Page 24: Ruby on Rails 3

Active Record: Observers• When the Model is in a clutter by callbacks

– Hard to read the Model

• Observers are callbacks but in a separate class– Good at readability

Separation of concerns

Page 25: Ruby on Rails 3

Active Record: Db Migrationrails g migration reviewrake db:migrate

Page 26: Ruby on Rails 3

Confidential 26

Controller

Page 27: Ruby on Rails 3

Controller• Handle requests and issue responses• Pull data from the Model• Choose the View to render• An action is a public method and/or a corresponding method

Page 28: Ruby on Rails 3

Controller: Rendering responses• A response is rendered with the render command• Redirects are made with the redirect_to command• An action can only render a response once

Page 29: Ruby on Rails 3

Controller: Filters• before_filter• after_filter• around_filter

Separation of cross-cutting concerns

Page 30: Ruby on Rails 3

Controller: Example• rails g controller article

Page 31: Ruby on Rails 3

Routing• Forward requests to action methods

Page 32: Ruby on Rails 3

Restful routes and resources

Page 33: Ruby on Rails 3

View

Page 34: Ruby on Rails 3

View• .html.erb: used for rendering HTML• .js.erb: used for AJAX functionality• .xml.builder: used for RSS/Atom

Page 35: Ruby on Rails 3

View• Helper methods to generate links, forms, and JavaScript, and

to format text• Partials are templates that render a part of a page• Layouts a templates that contains common page elements

Page 36: Ruby on Rails 3

Confidential 36

• Action Mailer• Bundler: manages dependencies• etc.

Others

Page 37: Ruby on Rails 3

Confidential 37

Discussion!http://github.com/hotrannam/barcamp