Introduction to Ruby on Rails
Total Page:16
File Type:pdf, Size:1020Kb
Introduction to Ruby on Rails Thomas Kowark Software Engineering II Prof. Plattner, Dr. Uflacker WS 2015/16 Enterprise Platform and Integration Concepts group Introduction to Ruby on Rails 1. Ruby & Ruby on Rails ■ What is Ruby on Rails? ■ A few words about Ruby ■ Rails' core components ■ RESTful architecture 2. Your first Rails applicaon 3. Your introductory Rails exercise 4. AddiAonal Literature Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 October 16, 2015 2 What is Ruby on Rails? Web applicaon development framework wriHen in Ruby ■ hHp://rubyonrails.org/ Philosophy ■ "Don't repeat yourself" – DRY ■ ConvenAon over Configuraon – there is "the Rails way" ■ RESTful architecture ■ Everything in its place Rails 1 Rails2 Rails 3 Rails 4 Rails 5 2003 2006 2009 2013 est. 2015 ■ Used by Github, Groupon, TwiHer (parAally) Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 October 16, 2015 3 A few words about Ruby hHp://www.ruby-lang.org/ ■ Dynamic, reflecAve, general-purpose, object-oriented ■ Influenced by Perl, Smalltalk, Eiffel, and Lisp ■ Open-source, mature soaware ■ Matz’s Ruby Interpreter (MRI) versions: Yukihiro "Matz" Matsumoto with R. Stallman Ruby 1.0 Ruby 1.8.7 Ruby 1.9.3 Ruby 2.0.0 Ruby 2.2.2 1996 2010 2011 2013 2015 □ AddiAonally different VMs available (JRuby, Rubinius, IronRuby, Maglev) Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 Image by Rubén Rodríguez (CC BY 3.0) - hHp://commons.wikimedia.org/wiki/File:Matz.jpgOctober 16, 2015 4 Rails Core Components Ac1on Pack Raili1es Ac1on View (core code, e.G. rake) View (renders template) Ac1ve Support Ac1on Dispatch (ulity classes, (parses HTTP, sessions, cookies, etc.) e.g. i18n) Controller Ac1on Controller (make data available, applica1on flow) Ac1on Mailer (email services) Ac1ve Model Model (e.G. validaons) Gems Ac1ve Record (ORM) (packaGed libraries) Data storage hps://rubygems.orG/ Database (SQL, Graph..) Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 hHp://adrianmejia.com/blog/2011/08/11/ruby-on-rails-architectural-design/ October 16, 2015 5 Rails Application Layout my_first_rails_app/ app/ assets/ controller/ applicaon_controller.rb helpers/ applicaon_helper.rb models/ views/ applicaon/ index.html.erb layouts/ applicaon.html.erb Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 October 16, 2015 6 RESTful Architecture ■ Representaonal State Transfer (REST) is a soaware architecture style for distributed systems ■ Principles □ Uniform Interface □ Stateless InteracAons □ Cacheable □ Clients and servers □ Layered System □ Code on Demand (opAonal) ■ Largest RESTful implementaon: World Wide Web Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 October 16, 2015 7 RESTful Architecture – HTTP verbs ■ REST supports all 4 HTTP 1.1 verbs: GET, PUT, POST, DELETE ■ DifferenAaon of collecAons and individual elements Resource GET PUT POST DELETE Single element Retrieve Update or create Create Delete hHp://localhost:3000/authors/1 Collecon List Replace Create Delete hHp://localhost:3000/authors Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 October 16, 2015 8 Examples of Routes ■ GET / # invoke “home” controller ■ GET /authors # retrieve a list of all authors ■ GET /authors/new # get the form to enter a new author ■ POST /authors # create a new author ■ GET /authors/1 # show details of the first author ■ GET /authors/1/edit # get the form to edit the first author ■ PUT /authors/1 # update the first author ■ DELETE /authors/1 # delete the first author Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 October 16, 2015 9 Introduction to Ruby on Rails 1. Ruby & Ruby on Rails 2. Your first Rails applica1on 3. Your introductory Rails exercise 4. AddiAonal Literature Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 October 16, 2015 10 How to Start? ■ Opon 1: You use Mac or Linux □ Install and use Ruby on Rails directly on your OS □ Ruby version manager (e.g. RVM, rbenv) if older versions of Ruby should be kept □ hHp://guides.rubyonrails.org/geng_started.html#installing-rails □ Or use opon 2 ■ Opon 2: You have Windows or want to use a VM (recommended) □ We prepared one for you via Vagrant (hHps://www.vagrantup.com/) □ Uses VirtualBox in the backend (free on all plaorms) (hHps://www.virtualbox.org/) □ Use your own tools & editors, run the project in a headless VM □ See project README for setup instrucAons ■ Opon 3: You have Windows and install Ruby on Rails directly on your OS □ Tends to consume some Ame, might cause problems with certain dependencies □ hHp://railsinstaller.org/en Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 October 16, 2015 11 Comprehensive RoR tutorial Recommended to work through / read this hands-on tutorial. Seriously. hHp://guides.rubyonrails.org/geng_started.html Info: Before you start coding, make sure, the correct versions are installed. $ ruby --version $ rails --version The following slides give a general overview Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 October 16, 2015 12 rails – Main executable Start interacAve shell to test out ideas $ rails console Start new rails applicaon $ rails new Generate boilerplate for models, controllers & views $ rails generate Start the development server $ rails server Start a direct database shell $ rails dbconsole ■ Example: generate model, controller and view without controller specs $ rails g scaffold author last_name:string homepage:string --controller-specs false Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 hHp://guides.rubyonrails.org/command_line.html October 16, 2015 13 Bundler – Ruby package manager ■ Ruby libraries are packaged as "gems" ■ Online repository at hps://rubygems.org/ ■ Bundler resolves dependencies of gems ■ Gemfile holds a list of required gems □ Specify versions, e.g. gem 'rails' >= '4.1.6’ □ Alt. sources, e.g. :github => ”tkowark/sawyer” ■ Gemfile.lock is populated with resolved dependencies □ Should be under version control Manually install a gem (Ruby package) $ gem install Info: Gemfile.lock Install all gems listed as depencies in Gemfile contains all the actually installed $ bundle install versions of gems. Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 October 16, 2015 14 rake – Ruby make List all available rake commands $ rake -T List all configured routes $ rake routes Setup the database and run all migraons $ rake db:setup && rake db:migrate Replace database with db layout from db/schema.rb Info: Do not run migraons. Running schema:load $ rake db:schema:load is advisable when seng up a completely new project. It is not Run Rspec (tesAng framework for RoR) tests intended to work around bad migraons. $ rake spec Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 October 16, 2015 15 Git – distributed version control system ■ Install Git: □ sudo apt-get install git □ hHp://git-scm.com/ (Installers for all systems) ■ Seng up user name and email: □ Mandatory to commit changes □ Use your github credenAals! $ git config --global user.email “[email protected]” $ git config --global user.name “Max Mustermann” ■ Alternave: seng parameters only for one project: $ cd /path/to/your/project $ git config user.email “[email protected]” $ git config user.name “Max Mustermann” Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 October 16, 2015 16 Git workflow – commiting a change Checkout remote repository to local copy $ git clone https://github.com/hpi-swt2/wimi-portal Change main layout template app/views/layouts/applicaon.html.erb Stage changes (add files from working copy to repository index) $ git add app/views/layouts List changes to be commiHed $ git status Commit with commit messages. Reference Github issue #25 $ git commit –m "Fixed issue #25" Fetch and merge changes from remote repository $ git pull Publish local commits $ git push Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 October 16, 2015 17 Introduction to Ruby on Rails 1. Ruby & Ruby on Rails 2. Your first Rails applicaon 3. Your introductory Rails exercise 4. AddiAonal Literature Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 October 16, 2015 18 Exercise 1 – Code School ■ Goals □ Get familiar with Ruby on Rails □ Create necessary accounts for the project ■ Tasks □ Create a Github account □ Link it with CodeSchool.com (unless you already have an account) □ Complete Rails coding school: hHp://railsforzombies.com □ Complete Git Tutorial: hHps://www.codeschool.com/courses/try-git □ When done, create a screenshot of your report card ■ Deadline: Nov 6, 11:59 pm CET (firm) Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 October 16, 2015 19 Exercise 2 - Develop a Rails App ■ Goals □ Deepen your understanding of Rails Apps □ Get used to TDD/BDD ■ Tasks □ Sign up on Github Classroom – hHps://classroom.github.com/assignment-invitaons/ebc02d7d20638a7ef2660434c04136c2 – Repository might take some me to receive iniAal skeleton code (be paent...) □ Make the tests “green” – Start with ‘rake spec’ – Run single feature tests with ‘rspec spec/features/…/..._spec.rb’ □ Commit your screenshot from Exercise 1 ■ Deadline □ Nov 6, 11:59 pm CET (firm) □ POs are exempt from this task. Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 October 16, 2015 20 Exercise 2 – Requirements ■ Web-based paper management system □ Author – First name – Last name – Homepage □ Paper – Title – Publicaon – Year □ A paper has many authors (max. 5) □ An author has many papers ■ We prepared a Rails applicaon for you Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 October 16, 2015 21 Exercise 2 – Use Cases & Site Map Home Show Show Papers Authors Add Author Add Paper Show Author Show Paper Details Details Edit Author Edit Paper Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 October 16, 2015 22 Exercise 2 – Mock Up hHps://gomockingbird.com/mockingbird/index.html?project=v890g6l Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 October 16, 2015 23 Additional Literature General literature ■ Ruby, S.; Thomas, D.; Hansson D.