1. What is scaffolding in Crails ?

Scaffolding is a simple way to generate a temporary structure for your Crails project. It is available through the Crails command line interface, and is typically used to generate new model, views and controllers.

1.0 Assumptions

This tutorial assumes that you're running an application with the odb database backend and using the html format. You can create such an application using the following command:

  crails new -n scaffold_test -d sqlite -p html

1.1 How to use the scaffold command

An example for scaffolding a blog project would look like this:

  crails scaffold resource -m Article 

This example creates:

This is pretty much everything you need to get started with a new resource. Last steps involve building the server and running the migration on the database:

  crails build                        # Builds your server and tasks
  build/tasks/odb_migrate/task -c odb # Creates or update the database schema
  build/server                        # Runs your server

You can now check out your new resources at http://localhost:3001/article

Scaffolding generates views for the formats enabled in your crails application. Meaning that depending on your project's settings, the scaffold resource command will generate views for the html format, the json format, or both formats.

1.2 Scaffolding with extra properties

In the previous example, we haven't defined any properties for our objects. You may add those yourself afterwards, but you can also define them directly during scaffolding, using the property option, such as:

  crails scaffold resource -m Article -p std::string/name std::string/contents std::time_t/date

If you scaffold your resource again like this, you'll notice your views now feature three fields. You should already be able to use these simple views to create, edit, list or destroy Article objects.

1.3 Generating specific components

You may not always need all the elements that the scaffold resource command provides. Which is why you can also create individual components, with more options. A scaffold resource basically amounts to running the following commands:

  crails scaffold controller -m Article
  crails scaffold model -m Article -p std::string/name std::string/contents std::time_t/date
  crails scaffold view -m Article -p std::string/name std::string/contents std::time_t/date

These commands also provide custom options that you can check out using:

  crails scaffold controller --help

2. Advanced scaffolding

1.1 Layouts

The scaffolding tool also provides a command to generate HTML layouts. If you are not yet comfortable with the notion of layouts, check out the Layouts and Rendering tutorial.

Here's a command that will generate a default layout for your application:

  crails scaffold layout

By default, this will download Bootstrap stylesheets in your app/assets/stylesheets folder, and generate a simple layout using this theme. The layout will be generated at app/views/layouts/application.html, and you can use it in your application by settings the layout shared variable from your controller, such as:

  vars["layout"] = std::string("layouts/application");
  // or alternatively, at render-time:
  render("article/show", {
    {"layout", std::string("layout/application")}
  });

If you'd rather not use Bootstrap, you can also use the following command to keep things simple:

  crails scaffold layout --toolkit none

1.2 Tasks

Tasks are useful when you want to run processes using your project's code, but outside of a web server. Here's how you can quickly create a task:

  crails scaffold task -n my_task

This will create the tasks/my_task folder, with minimal CMakeLists.txt and main.cpp files. The task will be built along with the server and all other targets defined in your main CMakeLists.txt. You may then run it as such:

  build/tasks/my_task/task

1.3 Modules

When your project gets bigger and bigger, you might want to compartimentalize your features differently. Modules are a good way to achieve that. Each module have their own set of assets, controllers, models and views folders, along with their own set of routes.

Here's how to scaffold a new module:

  crails scaffold module -n my_module

This command will create the modules/my_module folder, inculding:

The app/routes.cpp shall also be modified, so that your module's routes are wired into the project when the module is included in the build.