Here’s the code necessary to write a very simple CB app. Let’s suppose we have a database of library books, and want to write an application for viewing them.
First, we create a new Chicago Boss project. Under the Chicago Boss directory:
$ make app PROJECT=simple
This creates the directory simple
on the same level as the Chicago Boss installation.
Next we define a library_book
model. Under the simple
directory:
model/library_book.erl:
-module(library_book, [Id, Title, AuthorName]).
-compile(export_all).
Next we define a controller for pulling records from the database. We prefix the filename of our controller with the name of our project.
controller/simple_library_book_controller.erl:
-module(simple_library_book_controller, [Req]).
-compile(export_all).
list('GET', []) ->
LibraryBooks = boss_db:find(library_book, []),
{ok, [{books, LibraryBooks}]}.
Finally we make a template file that puts all of the data into a pretty table.
view/library_book/list.html:
<table>
<tr><td>Title</td><td>Author</td></tr>
{% for book in books %}
<tr><td>{{ book.title }}</td>
<td>{{ book.author_name }}</td>
</tr>
{% endfor %}
</table>
And that’s it! Visiting http://localhost:8001/library_book/list will show us all the library books in the database.