Intro to Rails Give Camp Atlanta


Published on

A presentation detailing building a Rails shopping cart from scratch

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • heroku create _NAME_ will create an app called _NAME_, i.e.
  • Modifying the specified line to include the :precision and :scale arguments.rake db:migrate executes this code to create the products tableThe scaffolding will now be available at http://localhost:3000/products
  • When you go to /products and click New ProductWhen you submit a product that fails validation, rails automatically generates the error page shown
  • Intro to Rails Give Camp Atlanta

    1. 1. Intro to Ruby on Rails<br />Jason Noble<br /><br />Alan Hecht<br /><br />
    2. 2. Slides are available<br /><br />Intro to Rails Give Camp Atlanta<br />Finished code is also available<br /><br />
    3. 3.
    4. 4. History<br />Initial Release July 2004<br />Written by David Heinemeier Hansson<br />Rails 3.0 released August 29, 2010<br />
    5. 5. Technical Overview<br />Model-View-Controller (MVC) architecture<br />Includes scaffolding to automatically construct the models and views needed for a basic site<br />Includes WEBrick, a simple Ruby web server<br />Includes Prototype and JavaScript libraries for Ajax<br />RESTful web services used instead of SOAP<br />Handles HTML and XML output out of the box<br />
    6. 6. Framework Structure<br />ActiveRecord – Object relational mapping system for database access<br />ActiveResource -- Web Services<br />ActionPack<br />ActiveSupport<br />ActionMailer<br />Lots of plugins available<br />
    7. 7. Philosophy<br />Convention over Configuration<br />Decreases decisions that developers need to make<br />By doing things the “standard” way it just works<br />Don’t Repeat yourself (DRY)<br />Every piece of code has a single, unambiguous authoritative representation within a system<br />Modifying any single element does not change other logically-unrelated elements<br />Use methods and subroutines to abstract out pieces of code called in multiple places<br />
    8. 8. Installing<br />Install Ruby and RubyGems<br />Gems are a Ruby package format<br />gem install rails<br />Rails works with SQLite out of the box<br />MySQL, PostgreSQL, Oracle, MS SQL gems available<br />
    9. 9. Installing Rails<br />Windows<br /><br />OS X<br /><br />Linux<br />Most of will work<br />
    10. 10. Let’s build a shopping cart<br />rails new demo_app<br />vi demo_app/.rvmrc<br />rvm –create use 1.9.2@rails3<br />cd demo_app<br />rails server<br />http://localhost:3000<br />gitinit .<br />git add .<br />git commit –m ‘Initial rails app’<br />
    11. 11. Push App to Heroku<br />vi Gemfile<br />Add “gem heroku” line<br />bundle install<br />heroku create<br />git push –u heroku master<br />heroku open<br />
    12. 12. Product Scaffolding<br />Add a CRUD (create, read, update, delete) interface<br />rails generate scaffold Product title:stringdescription:textimage_url:stringprice:decimal<br />creates db/migrate/20110108190442_create_products.rb<br />
    13. 13. db/migrate/2011010819_create_products.rb<br />Filename is date/time specific, so yours may differ<br />Adding precision and scale to price field<br />rake db:migrate<br />http://localhost:3000/products<br />
    14. 14. Modifying the view<br />app/views/products/_form.html.erb<br />Views are dynamically rendered, so reloading in your browser will show your updates<br />
    15. 15. Seeding your DB with data<br />db/seeds.rb<br />Executes Ruby code to pre-populate your database<br />Good way to start with sample data<br />rake db:seed<br />Runs the ruby code<br />
    16. 16. Update Products View<br />app/views/products/index.html.erb<br />
    17. 17. Push to Heroku<br />git add .<br />git commit –m ‘Products scaffold’<br />git push heroku<br />heroku rake db:migrate<br />heroku rake db:seed<br />heroku open<br />add a /products to the URL<br />
    18. 18. Data Validation<br />Rails has data validation built into the model<br />app/models/product.rb<br />
    19. 19. Data Validation (cont.)<br />
    20. 20. Rails Testing<br />rake test<br />Runs all the built in Rails tests<br />Prints Failures as it finds them, but continues<br />
    21. 21. Functional Tests<br />Tests Controller actions<br />test/functional/products_controller_test.rb<br />
    22. 22. Unit Tests<br />Tests Model methods/attributes/validations<br />test/unit/product_test.rb<br />
    23. 23. Push to Heroku<br />git add .<br />git commit –m ‘Added validation’<br />git push heroku<br />
    24. 24. Store Front<br />rails generate controller store index<br />http://localhost:3000/store/index<br />gitrm public/index.html<br />config/routes.rb<br />http://localhost:3000/<br />
    25. 25. Store Controller<br />Modify store controller to pull products from the database<br />app/controllers/store_controller.rb<br />
    26. 26. View file changes<br />app/views/store/index.html.erb<br />
    27. 27. Application Layout<br />app/views/layouts/application.html.erb<br />
    28. 28. Updated view<br />
    29. 29. Customer wants books alphabatized<br />app/models/product.rb<br />
    30. 30. Store Functional Test<br />test/functional/store_controller_test.rb<br />
    31. 31. Push to Heroku<br />git add .<br />git commit –m ‘Store scaffold’<br />git push heroku<br />heroku open<br />
    32. 32. Cart Scaffold<br />rails generate scaffold cart<br />rake db:migrate<br />
    33. 33. Modify Application Controller<br />app/controller/application_controller.rb<br />
    34. 34. LineItems scaffold<br /> rails generate scaffold line_itemproduct_id:integercart_id:integerquantity:integer<br />
    35. 35. Update Cart model<br />app/models/cart.rb<br />
    36. 36. Update LineItem Model<br />app/models/line_item.rb<br />
    37. 37. Update Product model<br />app/models/product.rb<br />
    38. 38. Add to Cart button<br />app/views/store/index.html.erb<br />
    39. 39. LineItems controller<br />app/controllers/line_items_controller.rb<br />
    40. 40. Carts View<br />app/views/carts/show.html.erb<br />
    41. 41. Carts Controller<br />app/controller/carts_controller.rb<br />
    42. 42. Update Heroku<br />git add .<br />git commit –m ‘Cart updated’<br />git push heroku<br />heroku open<br />
    43. 43. Further Reading<br />Agile Web Development with Rails<br /><br /><br />