Introduction to Ruby on Rails

67
Web Development using Ruby on Rails Vysakh Sreenivasan Submify.com vysakh.quora.com vysakh0 - twitter/facebook March 24, 2013

description

This presentation is done using Beamer. Source: https://github.com/vysakh0/beamer

Transcript of Introduction to Ruby on Rails

  • 1. Web Development using Ruby on Rails Vysakh Sreenivasan Submify.comvysakh.quora.com vysakh0 - twitter/facebook March 24, 2013

2. Introduction to RubyTable of Contents 1Introduction to Ruby Why Ruby? Features Getting Started Installation 2Introduction to Rails Why Rails? MVC Pattern Ruby utilities Installation REST 3NotesInstallationTutorialsHelpVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 2 / 34 3. Introduction to RubyVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 3 / 34 4. Introduction to Ruby Why Ruby?Why Ruby? enjoy programmingVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 4 / 34 5. Introduction to Ruby Why Ruby?Why Ruby? be productive enjoy programmingVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 4 / 34 6. Introduction to Ruby Why Ruby?Why Ruby? be productive enjoy programming be happyVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 4 / 34 7. Introduction to Ruby FeaturesHELLO WORLD #include int main(){ printf( Hello World ); return 0; }Vysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 5 / 34 8. Introduction to Ruby FeaturesHELLO WORLD import java.io.*; public class Example { public static void main(String[] args) { System.out.println( Hello World ); } }Vysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 6 / 34 9. Introduction to Ruby FeaturesSimplicityRubyputs Hello WorldVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 7 / 34 10. Introduction to Ruby FeaturesSimplicityRubyputs Hello WorldVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 7 / 34 11. Introduction to Ruby Features Easy to Learn Open Source Rich libraries Truly Object Oriented Less Coding with fewer bugs Helpful CommunityVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 8 / 34 12. Introduction to Ruby Features Easy to Learn Open Source Rich libraries Truly Object Oriented Less Coding with fewer bugs Helpful CommunityVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 8 / 34 13. Introduction to Ruby Features Easy to Learn Open Source Rich libraries Truly Object Oriented Less Coding with fewer bugs Helpful CommunityVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 8 / 34 14. Introduction to Ruby Features Easy to Learn Open Source Rich libraries Truly Object Oriented Less Coding with fewer bugs Helpful CommunityVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 8 / 34 15. Introduction to Ruby Features Easy to Learn Open Source Rich libraries Truly Object Oriented Less Coding with fewer bugs Helpful CommunityVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 8 / 34 16. Introduction to Ruby Features Easy to Learn Open Source Rich libraries Truly Object Oriented Less Coding with fewer bugs Helpful CommunityVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 8 / 34 17. Introduction to Ruby Getting Started Interactive Ruby Simple Syntax Variables Naming Convention ConstantsVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 9 / 34 18. Introduction to Ruby Getting Started Type small bits of ruby code, see it get executed Interactive Ruby Simple Syntax Variables Naming Convention ConstantsVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 9 / 34 19. Introduction to Ruby Getting Started Parenthesis, Braces, Tab spaces- Optional Interactive Ruby Simple Syntax Variables Naming Convention ConstantsVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 9 / 34 20. Introduction to Ruby Getting Started a = 5 b = 10 Interactive Rubyc = a + b d = 11.4 Simple Syntax word = Cool :) Variables Naming Convention ConstantsVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 9 / 34 21. Introduction to Ruby Getting Started Camel Case rstName = vysakh Interactive Rubyfirst_name=Vysakh Simple Syntax Variables Naming Convention ConstantsVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 9 / 34 22. Introduction to Ruby Getting Started Pi = 3.14 Interactive Ruby#Constants begin with Capital Simple Syntax Variables Naming Convention ConstantsVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 9 / 34 23. Introduction to Ruby Getting Started In Ruby, everything isVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 10 / 34 24. Introduction to Ruby Getting Started if 1 == 1puts 1 is equal to 1 elsif 1 != 1 Logical Operatorsputs 1 is not equal to 1 Loops else Listputs Something else Hash and Sybmolsend Functions ClassesVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 11 / 34 25. Introduction to RubyGetting Started for while times upto Logical Operators downto Loops step List until Hash and Sybmols Functions ClassesVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on RailsMarch 24, 2013 11 / 34 26. Introduction to Ruby Getting Started numbers = [1, 3, 55, 2999] fruits = [Mango, Apple] Logical Operators mixed = [1, Mango] Loops List Hash and Sybmols Functions ClassesVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 11 / 34 27. Introduction to Ruby Getting Started names = {first => Mark} #Strings are expensive Logical Operators # first is a string Loops # :first is a symbol List names = {:first => Mark} Hash and Sybmols Functions ClassesVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 11 / 34 28. Introduction to Ruby Getting Started def add a, ba+b end Logical Operators puts add 1, 2 Loops List Hash and Sybmols Functions ClassesVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 11 / 34 29. Introduction to Ruby Getting Started class Calc def add a, ba+b Logical Operators end Loops end List Hash and Sybmolsclass ChildCalc < Calc Functions end Classes c = Calc.new puts c.add 4, 5 child = ChildCalc.new puts child.add 3, 4Vysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 11 / 34 30. Introduction to Ruby InstallationInstallation Use Ruby Version Management ToolVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 12 / 34 31. Introduction to Ruby InstallationInstallation Use Ruby Version Management Tool RVM or rbenvVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 12 / 34 32. Introduction to RailsTable of Contents 1Introduction to Ruby Why Ruby? Features Getting Started Installation 2Introduction to Rails Why Rails? MVC Pattern Ruby utilities Installation REST 3NotesInstallationTutorialsHelpVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 13 / 34 33. Introduction to RailsVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 14 / 34 34. Introduction to Rails Why Rails?Why Rails? Extremely Productive Web Framework Develop 10 times faster than a Java Framework Rails promotes best practises Rails is very mature web framework Open Source, Vibrant Community Access to hundreds of gemsVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 15 / 34 35. Introduction to Rails Why Rails?Why Rails? Extremely Productive Web Framework Develop 10 times faster than a Java Framework Rails promotes best practises Rails is very mature web framework Open Source, Vibrant Community Access to hundreds of gemsVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 15 / 34 36. Introduction to Rails Why Rails?Why Rails? Extremely Productive Web Framework Develop 10 times faster than a Java Framework Rails promotes best practises Rails is very mature web framework Open Source, Vibrant Community Access to hundreds of gemsVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 15 / 34 37. Introduction to Rails Why Rails?Why Rails? Extremely Productive Web Framework Develop 10 times faster than a Java Framework Rails promotes best practises Rails is very mature web framework Open Source, Vibrant Community Access to hundreds of gemsVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 15 / 34 38. Introduction to Rails Why Rails?Why Rails? Extremely Productive Web Framework Develop 10 times faster than a Java Framework Rails promotes best practises Rails is very mature web framework Open Source, Vibrant Community Access to hundreds of gemsVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 15 / 34 39. Introduction to Rails Why Rails?Why Rails? Extremely Productive Web Framework Develop 10 times faster than a Java Framework Rails promotes best practises Rails is very mature web framework Open Source, Vibrant Community Access to hundreds of gemsVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 15 / 34 40. Introduction to Rails Why Rails?Rails Philosophy Convention over Conguration Donot Repeat Yourself (DRY) REST is the best pattern for web applicationsVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 16 / 34 41. Introduction to Rails Why Rails?Rails Philosophy Convention over Conguration Donot Repeat Yourself (DRY) REST is the best pattern for web applicationsVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 16 / 34 42. Introduction to Rails Why Rails?Rails Philosophy Convention over Conguration Donot Repeat Yourself (DRY) REST is the best pattern for web applicationsVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 16 / 34 43. Introduction to Rails Why Rails?FULL STACK FRAMEWORK.. WHAT?? It abstracts and manages all parts of a web application Database(model) HTML/visualization(view) Request ow(Controller)Vysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 17 / 34 44. Introduction to Rails Why Rails?FULL STACK FRAMEWORK.. WHAT?? It abstracts and manages all parts of a web application Database(model) HTML/visualization(view) Request ow(Controller)Vysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 17 / 34 45. Introduction to Rails Why Rails?FULL STACK FRAMEWORK.. WHAT?? It abstracts and manages all parts of a web application Database(model) HTML/visualization(view) Request ow(Controller)Vysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 17 / 34 46. Introduction to Rails MVC PatternMVC PatternVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 18 / 34 47. Introduction to Rails MVC PatternModel Maps a table in the database with a ruby object Validates your data before saving them Here you manipulate your data Here you add associationsVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 19 / 34 48. Introduction to Rails MVC PatternModel Maps a table in the database with a ruby object Validates your data before saving them Here you manipulate your data Here you add associationsVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 19 / 34 49. Introduction to Rails MVC PatternModel Maps a table in the database with a ruby object Validates your data before saving them Here you manipulate your data Here you add associationsVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 19 / 34 50. Introduction to Rails MVC PatternModel Maps a table in the database with a ruby object Validates your data before saving them Here you manipulate your data Here you add associationsVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 19 / 34 51. Introduction to Rails MVC PatternController Each method is a function that your application exposes Controls the ow of the request Exposes your data to viewsVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 20 / 34 52. Introduction to Rails MVC PatternController Each method is a function that your application exposes Controls the ow of the request Exposes your data to viewsVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 20 / 34 53. Introduction to Rails MVC PatternController Each method is a function that your application exposes Controls the ow of the request Exposes your data to viewsVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 20 / 34 54. Introduction to Rails MVC PatternViews Show data to the user (in various format: html, csv, pdf..)Vysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 21 / 34 55. Introduction to Rails MVC PatternRoutes It is a map between the HTTP request and the method of a controllerVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 22 / 34 56. Introduction to Rails MVC PatternMigration add column/remove column rename column/rename table change column create table/drop tableVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 23 / 34 57. Introduction to Rails Ruby utilitiesRake Rake a simple ruby build program with capabilities similar to make. Create a le Rakele namespace :my do task :alarm doputs Turned off alarm. end end Run this rake le with the command rake my:alarmVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 24 / 34 58. Introduction to Rails Ruby utilitiesgem and bundlergem A RubyGem is a software package, commonly called a gem.Gems contain a packaged Ruby application or library. bundler Bundler is the way to manage gem dependencies in RailsVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 25 / 34 59. Introduction to Rails InstallationInstallation and CreationInstalling gem install rails Creation rails new projectVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 26 / 34 60. Introduction to Rails RESTWhat is REST?Vysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 27 / 34 61. Introduction to Rails RESTRepresentational State transfer REST in terms of Rails boils down to two main principles: Using resource identiers such as URLs to represent resources. Transferring representations of the state of that resource between system components.Vysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 28 / 34 62. Introduction to Rails RESTRest - Example For example, the following HTTP request: DELETE /photos/17 would be understood to refer to a photo resource with the ID of 17, and to indicate a desired action: deleting that resource. In cong/routes le resources :photos In controller class PhotosController < ApplicationController def destroy end endVysakh Sreenivasan (vysakh.quora.com)Web Development using Ruby on Rails March 24, 2013 29 / 34 63. NotesTable of Contents 1Introduction to Ruby Why Ruby? Features Getting Started Installation 2Introduction to Rails Why Rails? MVC Pattern Ruby utilities Installation REST 3NotesInstallationTutorialsHelpVysakh Sreenivasan (vysakh.quora.com) Web Development using Ruby on Rails March 24, 2013 30 / 34 64. NotesInstallationInstallationrbenv https://github.com/sstephenson/rbenvRVM http://beginrescueend.com/Windows http://railsinstaller.org/Mac http://mxcl.github.com/homebrew/Vysakh Sreenivasan (vysakh.quora.com) Web Development using Ruby on Rails March 24, 2013 31 / 34 65. NotesTutorialsTutorials Ruby http://tryruby.org/ Rails Getting Started with Rails http://guides.rubyonrails.org/getting_started.html Michael Hartls Book http://guides.rubyonrails.org/getting_started.html Rails Zombies http://railsforzombies.com/ Rails Reference Railscasts http://railscasts.com/Vysakh Sreenivasan (vysakh.quora.com) Web Development using Ruby on Rails March 24, 2013 32 / 34 66. NotesHelpHelp Mailing list http://groups.google.com/group/rubyonrails-talk StackOverow http://stackoverflow.com/Vysakh Sreenivasan (vysakh.quora.com) Web Development using Ruby on Rails March 24, 2013 33 / 34 67. NotesHelpStart Coding! :)Vysakh Sreenivasan (vysakh.quora.com) Web Development using Ruby on Rails March 24, 2013 34 / 34