Ruby on Rails 3
Nam Ho
December 2010, BarCamp Saigon
2
About me• Working at KMS Technology
• A member of ALT.NET Saigon group
@hotrannam
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
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
Confidential 5
Getting started
6
Starting a new apprails new barcamp
7
Starting a new apprails server
8
Starting a new app
Confidential 10
Model
11
Model• Contains almost the application logic
– The heart of application
• Object Relational Mappings– Active Record– Data Mapper– Mongo Mapper
Active Recordrails g model Articlerake db:migraterails console
Table and Class name convention
The generated model class
The generated database migration file
Active Record: CRUDarticle = Article.newarticle.title = “Rails 3”article.body = “Barcamp Saigon”article.save
Create
Active Record: CRUDarticle = Article.firstarticle = Article.find(id)article = Article.find_by_title(“Rails 3”)articles = Artcile.all()
Lots of dynamic finders
Read
Active Record: CRUDarticle.title = “Web development with Rails 3”article.save
Update
Active Record: CRUDarticle.destroyArticle.delete(id)Article.delete_all(“created_at < '2011-01-01'")
Delete
Confidential 17
Active Record: Scope• A scope represents a narrowing of a database query
articles = Article.published.where_title(“Rails”)
Confidential 18
Active Record: Validationarticle.title = nilarticle.savearticle.errors.full_messages
Confidential 19
Active Record: Custom Val.comment = article.comments.create :body => “Great article!”comment.errors.full_messages
Confidential 20
Active Record: Relationship
1 : 1
Confidential 21
Active Record: Relationship
1 : n
Confidential 22
Active Record: Relationship
n : n
Active Record: Callbacks• before_create• after_create• before_save• after_save• before_destroy• after_destroy
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
Active Record: Db Migrationrails g migration reviewrake db:migrate
Confidential 26
Controller
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
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
Controller: Filters• before_filter• after_filter• around_filter
Separation of cross-cutting concerns
Controller: Example• rails g controller article
Routing• Forward requests to action methods
Restful routes and resources
View
View• .html.erb: used for rendering HTML• .js.erb: used for AJAX functionality• .xml.builder: used for RSS/Atom
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
Confidential 36
• Action Mailer• Bundler: manages dependencies• etc.
Others