Scaffolding a Rails App
- Understand the syntax of the
rails generate scaffoldcommand
- 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
- Start with the RailsSchool Starter App from last time (or your fork of it)
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
Examine the files created from the previous command
Run the pending database migration and start the rails server
rake db:migrate rails server
Explore the new
http://localhost:3000/postsaddress we can go to
Explore the output of
rake routesand the contents of the
Look at the controller methods in
app/controllers/posts_controller.rbas well as the view files in
Create the scaffolds that we need for our own projects. This will give them a domain model as well as a persistence layer.
- Rails Girls App Tutorial - this is a great tutorial. See also the links at the bottom.
- Official RailsGuides - we're really lucky to have such good official documentation with examples
- Michael Hartl's Rails Tutorial - this is a great, in-depth guide to building a full rails application and deploying it.
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.)