Rails Blog Scaffold


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:

> rails generate scaffold <capitalized singular model name> <optional first attribute name>:<first attribute type> <optional second attribute name>:<second attribute type> <optional third attribute name>:<third attribute type> <etc.>

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:

> rails generate scaffold <capitalized singular model name> <optional first attribute name>:<first attribute type> <optional second attribute name>:<second attribute type> <optional third attribute name>:<third attribute type> <etc.> --no-test-framework

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:

  1. an apartment model
  2. an apartments table with four columns (address, price, description, image_url)
  3. an apartments controller
  4. routes for apartments (show, new, index, update, edit, delete, create)
  5. views (show, new, edit, index)

To make all these in one command, you would run:

> rails generate scaffold Apartment address:string price:float description:text image_url:string

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:

> rails generate scaffold Apartment address:string price:float description:text image_url:string --no-test-framework

This will build everything except for the tests out for you. Not only will it build out the controller, migration, model, and views, it will also create a helper, some JSON builders, a stylesheet, and a CoffeeScript file. It will also add resources :apartments to the config/routes.rb file.

 create    db/migrate/20150117151933_create_apartments.rb
    create    app/models/apartment.rb
    invoke    test_unit
    invoke  resource_route
     route    resources :apartments
    invoke  scaffold_controller
    create    app/controllers/apartments_controller.rb
    invoke    erb
    create      app/views/apartments
    create      app/views/apartments/index.html.erb
    create      app/views/apartments/edit.html.erb
    create      app/views/apartments/show.html.erb
    create      app/views/apartments/new.html.erb
    create      app/views/apartments/_form.html.erb
    invoke    helper
    create      app/helpers/apartments_helper.rb
    invoke    jbuilder
    create      app/views/apartments/index.json.jbuilder
    create      app/views/apartments/show.json.jbuilder
    invoke  assets
    invoke    coffee
    create      app/assets/javascripts/apartments.js.coffee
    invoke    scss
    create      app/assets/stylesheets/apartments.css.scss
    invoke  scss

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:


View Rails Blog on Learn.co and start learning to code for free.

Unlock your future in tech
Learn to code.

Learn about Flatiron School's Mission

With a new take on education that falls somewhere between self-taught prodigy and four-year computer science degree, the Flatiron School promises to turn students with little programming experience into developers.

In the six months since the Manhattan coding school was acquired by WeWork, it has spawned locations in Washington, D.C., Brooklyn, and London. Now, WeWork is opening a fourth Flatiron School location, this time in Houston.

Adam Enbar, Flatiron School's cofounder, believes now is the time to grow. "How the world is changing has impacted working and learning in very similar ways. We think education fundamentally is about one thing: enabling people to pursue a better life."

Learn. Love. Code.
Students come to Flatiron School to change their lives. Join our driven community of career-changers and master the skills you need to become a software engineer or a data scientist.
Find Us