Server-side Web development via Ruby on Rails


Published on

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Server-side Web development via Ruby on Rails

  1. 1. Ruby on Rails<br />Server-side web development <br />VladStoian<br />Victor Porof<br />
  2. 2. What we’ll talk about<br />What is Ruby on Rails?<br />A brief history<br />MVC architecture in a nutshell<br />Distinctive framework features<br />Why Ruby? Why Rails?<br />Demo<br />
  3. 3. What is RoR?<br />Open Source<br />Abstraction providing generic server-side functionality<br />Used for web application development<br />It’s a framework for the Ruby language<br /> don’t confuse it with plain Ruby!<br />
  4. 4. A brief history<br />Originates in David Heinemeier Hansson’s work<br />First released in July 2004<br />Apple ships RoR with Mac OS X Leopard since 2007<br />Major new developments with v2.3 in 2009<br />Templates<br />Generate skeleton applications<br />Custom gems and configurations<br />Latest: v3.0.5, 27 February 2011<br />
  5. 5. MVC architecture<br />RoR is based on the Model-View-Controller design<br />It’s an architectural development pattern<br />Widely used in many other frameworks:<br />Oracle Application Framework, Cocoon, JSF (Java)<br />ASP.NET (C#)<br />SproutCore, JavascriptMVC (Javascript)<br />Django, Pylons (Python)<br />CakePHP(PHP)<br />PureMVC (many languages)<br />
  6. 6.
  7. 7. MVC on Rails<br />Models are Ruby classes, used to store and validate data<br />Handles migrations<br />They talk to databases<br />MySQL<br />SQLite<br />PostgreSQL<br />NoSQL<br />MongoDB<br />Cassandra<br />“Chubby guy in the back room” crunching the numbers<br />
  8. 8. Model snippet<br />class Person < ActiveRecord::Base<br />validates_presence_of :name<br />has_many :wifes<br />end<br />Person.create(:name => ”Muhammad Ali”).valid? # => true<br />Person.create(:name => nil).valid? # => false<br />
  9. 9. Migration snippet<br />class AddReceiveNewsletterToUsers < ActiveRecord::Migration<br />defself.up<br />change_table :users do |t|<br />t.boolean :receive_newsletter, :default => false<br /> end<br />User.update_all ["receive_newsletter = ?", true]<br /> end<br />defself.down<br />remove_column :users, :receive_newsletter<br /> end<br />end<br />
  10. 10. MVC on Rails<br />What the user sees<br />HTML, CSS, XML, Javascript (jQuery)<br />JSON, RSS, Atom<br />Automatically generated “view-puppets”<br />Visual representation of data<br />Does not have direct access to the model!<br />It shouldn’t do lots of processing or calculation<br />
  11. 11. View snippet<br /> <!-- app/views/items/new.rhtml --><br /> <%= form_tag :action => “create” %><br /> Name: <%= text_field“item”, “name”%><br/><br />Email: <%= text_field“item”, ”email”%><br/><br /> Password: <%= hidden_field “item”, “password”%><br/><br /> <%= submit_tag %><br /> <%= end_form_tag %><br />
  12. 12. MVC on Rails<br />Gateway between the model and the view<br />Handles user requests<br />Does parsing and data submissions<br />Takes care of sessions, cookies<br />Works out what data to show and what views to render<br />”The best controller: it gives orders without knowing (or caring) how it gets done”<br />
  13. 13. Controller snippet<br />classItemsController < ApplicationController<br /> def edit<br /> @item = Item.find(params[:id])<br /> <br /> @item.update_attributes(params[:item])<br />redirect_to :action => 'edit', :id =><br /> end<br /> end<br /> end<br />
  14. 14. MVC on Rails<br />To summarize:<br />Keep your view code skinny<br />Keep your controller code skinny<br />The fatness is in the model<br />
  15. 15. Distinctive framework features<br />Code generators<br />Awesome built-in functions => rapid development<br />Tons of “gems” to choose from<br />Cross-platform compatibility<br />Automated operation (CRUD)<br />Create, Retrieve, Automate and Delete<br />Simplified testing (Rake script)<br />
  16. 16. Code generator snippet<br />#model generator (general)<br />ruby script/generate model model_name #(in v2.3)<br />rails generate model model_name #(in v3)<br />#model generator (example)<br />rails generate model user name:stringhashed_password:string<br />#controller generator (general)<br />ruby script/generate controller controller_namemethod_name(s) (in v2.3)<br />rails generate controller controller_namemethod_name(s) (in v3)<br />#controller generator (example)<br />rails generate controller store index<br />
  17. 17. RoR servers<br />Mongrel<br />Webbrick<br />Thin<br />Apache (mod_passanger)<br /> it’s very scalable!<br />
  18. 18. Famous projects on Rails<br />Twitter (microblogging)<br />Crunchbase (companies database)<br />BaseCamp (project management)<br />Hulu (online tv)<br /> (phone database)<br />Xing (business network)<br />
  19. 19. Why Ruby?<br />Interpreted language => greater flexibility<br />Provides JIT (just in time compilation)<br />Garbage collection<br />Able to generate code on the fly<br />Cleaner syntax (no more “Verbose verbose is too verbose for verbose”)<br />Many implementations:<br />Jruby, IronRuby, Rubinius(Ruby + C++), MacRuby (ObjC)<br />
  20. 20. Why Rails?<br />Based on Ruby<br />Easy to implement CGI scripts<br />Rapid web-application development<br />Designed to make programming work easier<br />Less coding, more functionality<br />Thousands of plugins<br />Don’t reinvent the wheel<br />
  21. 21.
  22. 22. Creating a blog with<br />