Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Build a Todo List Application with Rails 4 Build a Todo List Application with Rails 4 Generate a Scaffold

ODOT - where did all that code come from?

Got the scaffold up and running on a local server, and I was quite taken aback and baffled when I saw that a basic todo list application is already up and running! Where did all that come from? I was expecting to code that from scratch.

4 Answers

Hi Andrew -

Scaffolding is a quick way to get started in Rails. The scaffolding command provides a lot of basic functionality. When you run the scaffold command, it generates a Model, a Controller, and Views based on the name that you passed when you run the command. All this is set up in the appropriate parts of the file system.

Yes, you could code all of the basic set-up stuff yourself, but Rails scaffolding saves you a bit of time. You don't have to (and may not want to) use scaffolding. You can easily generate your Models, Controllers and Views with the rails generate command.

Best,

Ronald

Hi Harry,

Absolutely! The Rails scaffolding command gets you up & running very quickly. For instance, say you run the following command in the terminal: rails g scaffold test name:string email:string

With that one command, Rails creates a model named Test which has two columns: name and email, both with the datatype of "string." The Test model will map to a table named Tests in a database. The scaffold creates the migration file for you. It will also set up a corresponding controller, views, routes, your app directory, and other good stuff. Basically, you get a" working" outline of the MVC (the Rails framework).

Of course you will need to extensively edit or modify scaffolding code to customize your app. Scaffolds are just intended to get you started. You have to take it from there to create your awesomely functional web app.

The scaffolding also generates some placeholder (very basic) tests. Testing is a good thing. If you use Rspec for testing your code, you can configure the Rails generators to automatically generate basic tests for that gem. Pretty good for a one line command!

Bottom-line: all the automatically generated code you see is part of the magic and power of Rails as a framework. It introduces a great deal of configuration via convention ... aspects of a web app that would normally take a great deal of time and effort to develop on your own. Instead of fussing with configuration files, you can spend you time getting your domain logic and other important stuff right. It's genius and awesome.

I hope this makes things much clearer for you. Best of luck and happy coding!

Ronald

Harry Tran
Harry Tran
10,618 Points

Thank you Ronald I appreciate you following up on my question with a detailed answer to explain how all that code came about from what almost looks like thin air.

Harry Tran
Harry Tran
10,618 Points

So generate scaffold creates a simple editable database with table names and editable fields?

I too was taken aback by refreshing the localhost and seeing a fully working to do list without any coding on my part.

Harry, you're very welcome... Glad to help.

Best,

Ronald