43
Intro to Ruby on Rails Jason Noble http://jasonnoble.org Alan Hecht http://alanhecht.me

Intro to Rails Give Camp Atlanta

Embed Size (px)

Citation preview

Page 1: Intro to Rails Give Camp Atlanta

Intro to Ruby on Rails

Jason Noblehttp://jasonnoble.org

Alan Hechthttp://alanhecht.me

Page 2: Intro to Rails Give Camp Atlanta

Slides are available

• http://slideshare.net/jasonjnoble/– Intro to Rails Give Camp Atlanta

• Finished code is also available– https://github.com/jasonnoble/givecampatl-rails-demo

Page 3: Intro to Rails Give Camp Atlanta
Page 4: Intro to Rails Give Camp Atlanta

History

• Initial Release July 2004

• Written by David Heinemeier Hansson

• Rails 3.0 released August 29, 2010

Page 5: Intro to Rails Give Camp Atlanta

Technical Overview

• Model-View-Controller (MVC) architecture• Includes scaffolding to automatically construct

the models and views needed for a basic site• Includes WEBrick, a simple Ruby web server• Includes Prototype and Script.aculo.us

JavaScript libraries for Ajax• RESTful web services used instead of SOAP• Handles HTML and XML output out of the box

Page 6: Intro to Rails Give Camp Atlanta

Framework Structure

• ActiveRecord – Object relational mapping system for database access

• ActiveResource -- Web Services• ActionPack• ActiveSupport• ActionMailer• Lots of plugins available

Page 7: Intro to Rails Give Camp Atlanta

Philosophy

• Convention over Configuration– Decreases decisions that developers need to make– By doing things the “standard” way it just works

• Don’t Repeat yourself (DRY)– Every piece of code has a single, unambiguous

authoritative representation within a system– Modifying any single element does not change other

logically-unrelated elements– Use methods and subroutines to abstract out pieces of

code called in multiple places

Page 8: Intro to Rails Give Camp Atlanta

Installing

• Install Ruby and RubyGems– Gems are a Ruby package format

• gem install rails

• Rails works with SQLite out of the box– MySQL, PostgreSQL, Oracle, MS SQL gems

available

Page 10: Intro to Rails Give Camp Atlanta

Let’s build a shopping cart

• rails new demo_app• vi demo_app/.rvmrc– rvm –create use 1.9.2@rails3

• cd demo_app• rails server• http://localhost:3000• git init .• git add .• git commit –m ‘Initial rails app’

Page 11: Intro to Rails Give Camp Atlanta

Push App to Heroku

• vi Gemfile– Add “gem heroku” line

• bundle install• heroku create• git push –u heroku master• heroku open

Page 12: Intro to Rails Give Camp Atlanta

Product Scaffolding

• Add a CRUD (create, read, update, delete) interface

• rails generate scaffold Product title:string description:text image_url:string price:decimal

• creates db/migrate/20110108190442_create_products.rb

Page 13: Intro to Rails Give Camp Atlanta

db/migrate/2011010819_create_products.rb

• Filename is date/time specific, so yours may differ• Adding precision and scale to price field

• rake db:migrate

• http://localhost:3000/products

Page 14: Intro to Rails Give Camp Atlanta

Modifying the view

• app/views/products/_form.html.erb

• Views are dynamically rendered, so reloading in your browser will show your updates

Page 15: Intro to Rails Give Camp Atlanta

Seeding your DB with data

• db/seeds.rb– Executes Ruby code to pre-populate your

database– Good way to start with sample data

• rake db:seed– Runs the ruby code

Page 16: Intro to Rails Give Camp Atlanta

Update Products View• app/views/products/index.html.erb

Page 17: Intro to Rails Give Camp Atlanta

Push to Heroku

• git add .• git commit –m ‘Products scaffold’• git push heroku• heroku rake db:migrate• heroku rake db:seed• heroku open– add a /products to the URL

Page 18: Intro to Rails Give Camp Atlanta

Data Validation

• Rails has data validation built into the model• app/models/product.rb

Page 19: Intro to Rails Give Camp Atlanta

Data Validation (cont.)

Page 20: Intro to Rails Give Camp Atlanta

Rails Testing

• rake test– Runs all the built in Rails tests– Prints Failures as it finds them, but continues

Page 21: Intro to Rails Give Camp Atlanta

Functional Tests

• Tests Controller actions• test/functional/products_controller_test.rb

Page 22: Intro to Rails Give Camp Atlanta

Unit Tests

• Tests Model methods/attributes/validations• test/unit/product_test.rb

Page 23: Intro to Rails Give Camp Atlanta

Push to Heroku

• git add .• git commit –m ‘Added validation’• git push heroku

Page 24: Intro to Rails Give Camp Atlanta

Store Front

• rails generate controller store index• http://localhost:3000/store/index• git rm public/index.html• config/routes.rb

• http://localhost:3000/

Page 25: Intro to Rails Give Camp Atlanta

Store Controller

• Modify store controller to pull products from the database

• app/controllers/store_controller.rb

Page 26: Intro to Rails Give Camp Atlanta

View file changes

• app/views/store/index.html.erb

Page 27: Intro to Rails Give Camp Atlanta

Application Layout

• app/views/layouts/application.html.erb

Page 28: Intro to Rails Give Camp Atlanta

Updated view

Page 29: Intro to Rails Give Camp Atlanta

Customer wants books alphabatized

• app/models/product.rb

Page 30: Intro to Rails Give Camp Atlanta

Store Functional Test

• test/functional/store_controller_test.rb

Page 31: Intro to Rails Give Camp Atlanta

Push to Heroku

• git add .• git commit –m ‘Store scaffold’• git push heroku• heroku open

Page 32: Intro to Rails Give Camp Atlanta

Cart Scaffold

• rails generate scaffold cart• rake db:migrate

Page 33: Intro to Rails Give Camp Atlanta

Modify Application Controller

• app/controller/application_controller.rb

Page 34: Intro to Rails Give Camp Atlanta

LineItems scaffold

• rails generate scaffold line_item product_id:integer cart_id:integer quantity:integer

Page 35: Intro to Rails Give Camp Atlanta

Update Cart model

• app/models/cart.rb

Page 36: Intro to Rails Give Camp Atlanta

Update LineItem Model

• app/models/line_item.rb

Page 37: Intro to Rails Give Camp Atlanta

Update Product model

• app/models/product.rb

Page 38: Intro to Rails Give Camp Atlanta

Add to Cart button

• app/views/store/index.html.erb

Page 39: Intro to Rails Give Camp Atlanta

LineItems controller

• app/controllers/line_items_controller.rb

Page 40: Intro to Rails Give Camp Atlanta

Carts View

• app/views/carts/show.html.erb

Page 41: Intro to Rails Give Camp Atlanta

Carts Controller

• app/controller/carts_controller.rb

Page 42: Intro to Rails Give Camp Atlanta

Update Heroku

• git add .• git commit –m ‘Cart updated’• git push heroku• heroku open

Page 43: Intro to Rails Give Camp Atlanta

Further Reading

• Agile Web Development with Rails– http://pragprog.com/titles/rails4/agile-web-devel

opment-with-rails• http://rubyonrails.org• http://railsforzombies.org/• http://tryruby.org/• http://rubykoans.com/