Download - Ruby on Rails 3
![Page 1: Ruby on Rails 3](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/1.jpg)
Ruby on Rails 3
Nam Ho
December 2010, BarCamp Saigon
![Page 2: Ruby on Rails 3](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/2.jpg)
2
About me• Working at KMS Technology
• A member of ALT.NET Saigon group
@hotrannam
![Page 3: Ruby on Rails 3](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/3.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/4.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/5.jpg)
Confidential 5
Getting started
![Page 6: Ruby on Rails 3](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/6.jpg)
6
Starting a new apprails new barcamp
![Page 7: Ruby on Rails 3](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/7.jpg)
7
Starting a new apprails server
![Page 8: Ruby on Rails 3](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/8.jpg)
8
Starting a new app
![Page 10: Ruby on Rails 3](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/10.jpg)
Confidential 10
Model
![Page 11: Ruby on Rails 3](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/11.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/12.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/13.jpg)
Active Record: CRUDarticle = Article.newarticle.title = “Rails 3”article.body = “Barcamp Saigon”article.save
Create
![Page 14: Ruby on Rails 3](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/15.jpg)
Active Record: CRUDarticle.title = “Web development with Rails 3”article.save
Update
![Page 16: Ruby on Rails 3](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/16.jpg)
Active Record: CRUDarticle.destroyArticle.delete(id)Article.delete_all(“created_at < '2011-01-01'")
Delete
![Page 17: Ruby on Rails 3](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/17.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/18.jpg)
Confidential 18
Active Record: Validationarticle.title = nilarticle.savearticle.errors.full_messages
![Page 19: Ruby on Rails 3](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/19.jpg)
Confidential 19
Active Record: Custom Val.comment = article.comments.create :body => “Great article!”comment.errors.full_messages
![Page 20: Ruby on Rails 3](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/20.jpg)
Confidential 20
Active Record: Relationship
1 : 1
![Page 21: Ruby on Rails 3](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/21.jpg)
Confidential 21
Active Record: Relationship
1 : n
![Page 22: Ruby on Rails 3](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/22.jpg)
Confidential 22
Active Record: Relationship
n : n
![Page 23: Ruby on Rails 3](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/23.jpg)
Active Record: Callbacks• before_create• after_create• before_save• after_save• before_destroy• after_destroy
![Page 24: Ruby on Rails 3](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/24.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/25.jpg)
Active Record: Db Migrationrails g migration reviewrake db:migrate
![Page 26: Ruby on Rails 3](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/26.jpg)
Confidential 26
Controller
![Page 27: Ruby on Rails 3](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/27.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/28.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/29.jpg)
Controller: Filters• before_filter• after_filter• around_filter
Separation of cross-cutting concerns
![Page 30: Ruby on Rails 3](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/30.jpg)
Controller: Example• rails g controller article
![Page 31: Ruby on Rails 3](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/31.jpg)
Routing• Forward requests to action methods
![Page 32: Ruby on Rails 3](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/32.jpg)
Restful routes and resources
![Page 33: Ruby on Rails 3](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/33.jpg)
View
![Page 34: Ruby on Rails 3](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/34.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/35.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022050815/540449288d7f729b768b4682/html5/thumbnails/36.jpg)
Confidential 36
• Action Mailer• Bundler: manages dependencies• etc.
Others