[ToDoList] Scaffold: The Voldemort of Magic Rails Generators

  1. The Scaffold Generator
  2. Unpacking Scaffold

 


 

There is something to be said for generating your new app and building it up from scratch - there is a personal touch there that should not be understated. We have just built a basic application essentially from scratch and, if you followed the last section, built bits of it again using a bit of help from Rails.
But an application framework that relies so heavily on convention should also have means of automating most of the construction of an app and, oh boy, does Rails have this.

From its debut, Rails has boasted that it knew what you wanted on your web application simply by the contents of your database. You might have already deduced that this is correct from the magic resources line in the routes configuration, and the routes table it generates, which provides the CRUD routes required by an MVC application.
However this flex is fully justified with the unholy power of its primary generator: scaffold. With this, we can run just commands in our dev environment's command prompt to create a fully working Rails app in our browser - this takes maybe 2 minutes (5 for a slower system / typist).

I invite you to cd back into your projects directory, try it and bear witness:

$ rails new to_do_list3                                       ## initialise our application
$ cd to_do_list3                                              ## head into our app dir
$ rails generate scaffold Todo name:string description:text   ## witchcraft
$ rails db:migrate                                            ## setup the database
$ rails server                                                ## start the server
Now if you're anything like me, when you navigate to http://host:3000/todos (where, again, the host if you chosen environment's base URL) you will go into a mild state of shock, followed by grief for the belief you previously harboured that developing web applications was difficult for anyone without a brain the size of Brazil.

Rails has done practically everything that it has taken us many sections of guide so far to do.

 


 

If we look back at the output of the scaffold generator command we ran, we will see how it achieved this:

      invoke  active_record
      create    db/migrate/20210521162926_create_todos.rb
      create    app/models/todo.rb
      invoke    test_unit
      create      test/models/todo_test.rb
      create      test/fixtures/todos.yml
      invoke  resource_route
       route    resources :todos
      invoke  scaffold_controller
      create    app/controllers/todos_controller.rb
      invoke    erb
      create      app/views/todos
      create      app/views/todos/index.html.erb
      create      app/views/todos/edit.html.erb
      create      app/views/todos/show.html.erb
      create      app/views/todos/new.html.erb
      create      app/views/todos/_form.html.erb
      invoke    resource_route
      invoke    test_unit
      create      test/controllers/todos_controller_test.rb
      create      test/system/todos_test.rb
      invoke    helper
      create      app/helpers/todos_helper.rb
      invoke      test_unit
      invoke    jbuilder
      create      app/views/todos/index.json.jbuilder
      create      app/views/todos/show.json.jbuilder
      create      app/views/todos/_todo.json.jbuilder
      invoke  assets
      invoke    scss
      create      app/assets/stylesheets/todos.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.scss
There's a lot to unpack here, but you'll recognise a lot of it from the generators we ran in the previous section:
  • Invocation of ActiveRecord to create the DB migration file and the Todo model
  • Invocation of TestUnit to create the tests for this model.
  • Invocation of ResourceRoute to automatically write our magic resource line to the routes config.
  • Creation of the TodosController, pre-filled with all the actions and logic we need.
    • Tests for the controller and its actions / views.
  • Invocation of erb to create all the necessary views, pre-filled with enough HTML to fulfill all CRUD actions.
  • Additional assets such as helpers and stylesheets.
  • Creation of json.jbuilder files as templates for JSON (API) responses.

The only thing that this does not include, other than the love of a human hand (which sounded wrong, I know) is a root route, simply because Rails doesn't know whether you want to generate your own additional pages for your homepage (this is actually a good thing), as well as data validation in the model (again, because Rails doesn't know what you need validation for).

In short, scaffold creates literally everything you need to create a datastore that you specify and interact with it from both the backend and frontend. If this isn't magic, I don't know what is.

 
 

Comments

Popular posts from this blog

[ToDoList] Basic Pages

New Rails Apps with Docker Compose

[ToDoList] Building the App