Rails Blog scaffold
Instructions
This is a simple lab. All you have to do is get the tests passing. There are a bunch of tests, but it should take you less than 5 minutes to finish this lab. Try to figure out the one command you need to run to make the entire suite pass. Read the Controller and Views specs for help on figuring out this one command. Try running rspec. You'll most likely get an error. You'll need to also remember to migrate your test database, but rspec will clue you in. Browse the code, read, try to take it all in.
The model name will be Post
and the attributes will be title
and description
. The title
attribute should be of type string
and description
should be of type text
.
What are all these files?
All the files that you currently see in your project directory, with the exception of a few spec
files, were generated via the rails new
command. Consequently, it built a fully fledged web app ready out of the box. This is an example of why the Rails framework is called an opinionated framework that favors convention over configuration. With Sinatra, we can build an app from scratch by just adding require 'sinatra'
in a file and calling rackup
. Rails is much heavier than that. With an app ready out of the box through generators like rails new
and rails generate scaffold
, we're able to build larger scale applications very quickly.
Rails Scaffolding
Scaffolding is a powerful tool that Rails provides. Here's the syntax:
For the purposes of all Rails labs at Flatiron, you'll be adding the flag --no-test-framework
to every command that includes "generate" or "g" for short. Therefore, the Flatiron version is:
For instance, say you were making an app to help New Yorkers find apartments. Your apartments should have an address (string), a price (float), a description (text), and an image url (string). You want to make the following things:
an apartment model
an apartments table with four columns (address, price, description, image_url)
an apartments controller
routes for apartments (show, new, index, update, edit, delete, create)
views (show, new, edit, index)
To make all these in one command, you would run:
The above command would also build out tests for your views, controller, etc. If you were scaffolding Apartments for a Flatiron lab, you would add that --no-test-framework
flag, so the command would be:
If you forget to add the --no-test-framework
argument, you will have to comb through your local repo and manually delete all of the newly-created, unnecessary tests. This is not fun! Please, remember the --no-test-framework
argument.
Browse through the Rails Guides below:
Resources
Last updated