San Francisco
April 2, 2015

Scaffolding a Rails App

Last week we focused on prototyping the front-end of our applications, but we weren't able to put real data into our views. This week we'll be focusing on the data, and how to create a rich data model for our applications.

Learning Objectives

  • Understand the syntax of the rails generate scaffold command
  • Understand what Models are, and how they're related to the database
  • Start to see the power of ActiveRecord
  • Start to gain an understanding of the purpose of a Controller
  • Learn the different HTTP actions, and how they map to routes, controllers, and views

Lesson Outline

  1. Start with the RailsSchool Starter App from last time (or your fork of it)
  2. Add a new resource to your app. For our example, say we want to have a simple blog post that's been created by a user (the starter app comes with a very simple user model)

    rails generate scaffold post title:string body:text user:references

  3. Examine the files created from the previous command

  4. Run the pending database migration and start the rails server

    rake db:migrate rails server

  5. Explore the new http://localhost:3000/posts address we can go to

  6. Explore the output of rake routes and the contents of the config/routes.rb file

  7. Look at the controller methods in app/controllers/posts_controller.rb as well as the view files in app/views/posts/

  8. Create the scaffolds that we need for our own projects. This will give them a domain model as well as a persistence layer.

Resources

Codewars Challenge

Since we're going to start using Ruby soon, I've created a simple ruby codewars kata to do before this class, so you can start the learning process.

Be sure to complete the following Codewars challenge before class! (If your RailsSchool profile includes your Codewars username, you'll get a nifty star if you complete the challenge.)
http://www.codewars.com/kata/convert-a-boolean-to-a-string/train/ruby



2:30pm Pacific - 4:30pm Pacific on April 2, 2015 at Coshx Labs
7 students were there

Whiteboard

Please, sign in to see the Whiteboard

Watch Lesson