Simple scaffold application in Rails

In last few hours, I was creating some application that could work in near future as a blog. But before that good to make some nice structure to have a base of application. So this post is about: how to create a blog using scaffolding. Scaffolding is a quick way to generate some of the most important pieces of an application. If I want to create models, view and also controllers for a new resource in a single operation scaffolding is a great way to make the job quick and easier.

Creating a new rails app is very easy. To do that, I can type only:

rails new scaffold_app

The whole structure of app is as:

To create an MVC components needed to post comments in the scaffold app directory we need to use scaffold generator:

rails generate scaffold post title:string body:text

And this generator creates post:

rails generate scaffold comment post_id:integer body:text

Creates post and comments tables in database:

rake db:migrate

Rails uses rake commands to run migrations:

Rake routes are the lists of all the URLs currently uses in the application.

zrzut-ekranu-2016-11-21-o-01-09-00 zrzut-ekranu-2016-11-21-o-01-11-43zrzut-ekranu-2016-11-21-o-01-09-05

 

After running rails server we can see that some very simple post and comments formulas were created. Good idea is to add some user with some unique id – integer type, public name – string type, email address – string type that will be used as a username. id, name, email attributes are columns in users table in a database.

To user should be associated each post that user will create:

rails generate scaffold Micropost content:string user_id:integer

So, for now, we should have scaffold application with:

  • comments
  • posts
  • users
  • and adding each of them to database.

Reference: