Intro to Rails Give Camp Atlanta
-
Upload
jason-noble -
Category
Technology
-
view
2.317 -
download
2
Transcript of Intro to Rails Give Camp Atlanta
Intro to Ruby on Rails
Jason Noblehttp://jasonnoble.org
Alan Hechthttp://alanhecht.me
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
History
• Initial Release July 2004
• Written by David Heinemeier Hansson
• Rails 3.0 released August 29, 2010
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
Framework Structure
• ActiveRecord – Object relational mapping system for database access
• ActiveResource -- Web Services• ActionPack• ActiveSupport• ActionMailer• Lots of plugins available
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
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
Installing Rails
• Windows– http://pragmaticstudio.com/blog/2010/9/23/install-r
ails-ruby-windows
• OS X– http://pragmaticstudio.com/blog/2010/9/23/install-r
ails-ruby-mac
• Linux– Most of
http://pragmaticstudio.com/blog/2010/9/23/install-rails-ruby-mac will work
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’
Push App to Heroku
• vi Gemfile– Add “gem heroku” line
• bundle install• heroku create• git push –u heroku master• heroku open
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
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
Modifying the view
• app/views/products/_form.html.erb
• Views are dynamically rendered, so reloading in your browser will show your updates
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
Update Products View• app/views/products/index.html.erb
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
Data Validation
• Rails has data validation built into the model• app/models/product.rb
Data Validation (cont.)
Rails Testing
• rake test– Runs all the built in Rails tests– Prints Failures as it finds them, but continues
Functional Tests
• Tests Controller actions• test/functional/products_controller_test.rb
Unit Tests
• Tests Model methods/attributes/validations• test/unit/product_test.rb
Push to Heroku
• git add .• git commit –m ‘Added validation’• git push heroku
Store Front
• rails generate controller store index• http://localhost:3000/store/index• git rm public/index.html• config/routes.rb
• http://localhost:3000/
Store Controller
• Modify store controller to pull products from the database
• app/controllers/store_controller.rb
View file changes
• app/views/store/index.html.erb
Application Layout
• app/views/layouts/application.html.erb
Updated view
Customer wants books alphabatized
• app/models/product.rb
Store Functional Test
• test/functional/store_controller_test.rb
Push to Heroku
• git add .• git commit –m ‘Store scaffold’• git push heroku• heroku open
Cart Scaffold
• rails generate scaffold cart• rake db:migrate
Modify Application Controller
• app/controller/application_controller.rb
LineItems scaffold
• rails generate scaffold line_item product_id:integer cart_id:integer quantity:integer
Update Cart model
• app/models/cart.rb
Update LineItem Model
• app/models/line_item.rb
Update Product model
• app/models/product.rb
Add to Cart button
• app/views/store/index.html.erb
LineItems controller
• app/controllers/line_items_controller.rb
Carts View
• app/views/carts/show.html.erb
Carts Controller
• app/controller/carts_controller.rb
Update Heroku
• git add .• git commit –m ‘Cart updated’• git push heroku• heroku open
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/