Introduction to Ruby on Rails

2,971 views
2,849 views

Published on

Introduction to Ruby on Rails (English + Indonesian)

Published in: Technology
3 Comments
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total views
2,971
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
Downloads
237
Comments
3
Likes
0
Embeds 0
No embeds

No notes for slide

Introduction to Ruby on Rails

  1. 1. by niwat0ri ( diki@diki.or.id )
  2. 2. WHO AM I ? research & development director at pt jerbee indonesia a newbie ruby programmer a foss & gnu/linux enthusiast a blogger a comic artist (chickenstrip.org) member of id-ruby community
  3. 3. WHY RUBY ON RAILS? kenapa ruby on rails?
  4. 4. buzzword, trend, hype kok bisa? ada apa? cuma euforia? apa memang bagus? ruby on rails that... ruby on rails this... ruby on rails that... ruby on rails that... ruby on rails this... ruby on rails that...
  5. 5. code your idea ASAP! technopreuneur, ubah ide anda menjadi web secepatnya !
  6. 6. powered by ruby on rails top web berbasis rails berdasarkan alexa rating oktober 2008
  7. 7. powered by ruby on rails non-publik? proyek? jobs? backend? intranet? outsorcing?
  8. 8. web dev. competition peluang, mengejar ketinggalan, persaingan, learning curve
  9. 9. programmer happiness ruby, convention over configuration, don't repeat yourself, fun
  10. 10. one man army start up kecil & solo freelancer harus agile !
  11. 11. good & clean code productivity, ruby's OOP, built in AJAX, RESTful, MVC, testing
  12. 12. WHAT IS RUBY ON RAILS? ok, kalau begitu apa sih ruby on rails itu?
  13. 13. WEB APP. = NOODLE OLD MANUAL PROCESS framework means... FASTER
  14. 14. WEB APP. FRAMEWORK MENU JAVA PYTHON PHP RUBY apa selera anda? Struts etc. Django etc. CodeIgniter etc. Ruby on Rails
  15. 15. MVC model - view - controller
  16. 16. the rails way ! convention over configuration (COC) ada struktur, penamaan dan aturan-aturan yang berlaku don't repeat yourself (DRY) menghemat waktu, mudah di-manage, reusable code
  17. 17. script/console macho & detail bersama command line
  18. 18. rails example script/generate model User name:string address:string birthday:date rake db:migrate script/console Loading development environment (Rails 2.1.0) >> User => User(id: integer, name: string, address:string, birthday:date)
  19. 19. $> errr... ? allergic to console? IDE also available
  20. 20. script/generate hasilkan code sakti secara instant
  21. 21. script/generate model Friendship user_id:integer friend_id:integer relation:string script/destroy model User script/generate scaffold User name:string address:string birthday:date
  22. 22. Create Read Update Delete
  23. 23. script/server aneka server & skenario deployment
  24. 24. script/server script/server -e production -p 4000 Server: Apache, WEBrick, Mongrel, nginx, Lighttpd Environment: development, production, test
  25. 25. script/plugins aneka rupa reusable code tersedia
  26. 26. script/plugins install restful_authentication script/plugin install git://github.com/ thoughtbot/paperclip.git gem install aka_time
  27. 27. layout rendering, images, css, javascript
  28. 28. MVC MODEL - view - controller
  29. 29. ActiveRecord Object Relation Mapping (ORM) Rails MVC's model foundation Database Agnostic Tables map to classes Rows map to objects Columns map to object attributes Convention: table = plural model = singular with capital
  30. 30. User.find(1) # SELECT * FROM users WHERE id=1 User.find_by_name 'Icha Aprillia' # SELECT * FROM users WHERE name='Icha Aprillia' User.find_by_login_and_email 'icha', 'icha92@yahoo.co.id' # SELECT * FROM users WHERE login='icha' AND emai='icha92@yahoo.co.id' User.find :all, :order => 'login DESC' # SELECT * FROM users ORDER BY login DESC User.find :all, :conditions => [quot;login=?quot;,quot;ichaquot;] # SELECT * FROM users WHERE login='icha'
  31. 31. Model's Associations Friend = User User id name address birthday id name address birthday Friendship id user_id friend_id relation
  32. 32. MODEL'S ASSOCIATIONS class User < ActiveRecord::Base has_many :friendships has_many :friends, :through => :friendships end class Friendship < ActiveRecord::Base belongs_to :user belongs_to :friend, :class_name => quot;Userquot;, :foreign_key => quot;friend_idquot; end User_1 had a friendship with his friend, the User_2. User_1 could have many friendship with his friends.
  33. 33. User.find(1).friends # SELECT * FROM users INNER JOIN friendships ON users.id = friendships.friend_id WHERE ((friendships.user_id = 1) # Listing User_1's friends User.find(1).friends.count # The numbers of User_1's friends
  34. 34. MVC model - view - CONTROLLER
  35. 35. ActionController Separation of business logic & presentation No logic in view! (Not so) Convention: Controller name = plural
  36. 36. CONTROLLERS class UsersController < ApplicationController def show @user = User.find(params[:id]) @friends = User.find(params[:id]).friends end ... Action “show” will be collecting user information and all of user's friends information.
  37. 37. MVC model - VIEW - controller
  38. 38. VIEW <div id=quot;profilequot;> <h2><%= @user.name %>'s Profile</h2> <p> <%=h @user.name %> <b>Name:</b> <%=h @user.name %> </p> <p> <b>Address:</b> <%=h @user.address %> <%= link_to friend.name, </p> <p> :controller => quot;usersquot;, :action => quot;showquot;, <b>Birthday:</b> <%=h @user.birthday %> </p> <%= link_to 'Edit', edit_user_path(@user) %> | <%= link_to 'Back', users_path %> </div> :id => friend.id %> <div id=quot;friendquot;> <h2><%= @user.name %>'s Friends</h2> <ul> <% for friend in @friends %> <li><%= link_to friend.name, :controller => quot;usersquot;, :action => quot;showquot;, :id => friend.id %></li> <% end %> </ul> </div> # app/views/user/show.html.erb.
  39. 39. HOW TO START? mulai dari mana ya?
  40. 40. DEMO & DISCUSSION sesi demonstrasi & diskusi
  41. 41. Thank You ! http://www.slideshare.net/niwat0ri Diki Andeas aka. niwat0ri Email: diki@diki.or.id , niwatori@gmail.com Website: www.chickenstrip.org Free images taken from www.sxc.hu Flickr collection of www.flickr.com/photos/ikhlasulamal

×