This trivial example demonstrates how to define, create, save, and display relational records for the obligatory blog application using Chicago Boss.
Model/blog_post.erl:
-module(blog_post, [Id, Title, Text, AuthorId]).
-compile(export_all).
-belongs_to(author).
-compile(export_all).
-belongs_to(author).
Model/author.erl:
-module(author, [Id, Name]).
-compile(export_all).
-has_many(blog_posts).
-compile(export_all).
-has_many(blog_posts).
Web/blog_post_controller.erl:
-module(blog_post_controller, [Req]).
-compile(export_all).
create('GET', []) ->
ok;
create('POST', []) ->
FakeAuthor = (author:new(id, "Mark Twain")):save(),
BlogPost = blog_post:new(id,
Req:get_post_value("title"),
Req:get_post_value("text"),
FakeAuthor:id()),
SavedBlogPost = BlogPost:save(),
{redirect, "/blog_post/view/"++SavedBlogPost:id()}.
view('GET', [BlogPostId]) ->
{ok, [{blog_post, boss_db:find(BlogPostId)}]}.
-compile(export_all).
create('GET', []) ->
ok;
create('POST', []) ->
FakeAuthor = (author:new(id, "Mark Twain")):save(),
BlogPost = blog_post:new(id,
Req:get_post_value("title"),
Req:get_post_value("text"),
FakeAuthor:id()),
SavedBlogPost = BlogPost:save(),
{redirect, "/blog_post/view/"++SavedBlogPost:id()}.
view('GET', [BlogPostId]) ->
{ok, [{blog_post, boss_db:find(BlogPostId)}]}.
Web/blog_post_views/create.html:
<form method="post">
Title: <input name="title"><br />
Text: <textarea name="text"></textarea><br />
<input type="submit">
</form>
Title: <input name="title"><br />
Text: <textarea name="text"></textarea><br />
<input type="submit">
</form>
Web/blog_post_views/view.html:
<h1>{{ blog_post.title }}</h1>
By {{ blog_post.author.name }}
(Post #{{ blog_post.author.blog_posts|length }})
<p>{{ blog_post.text }}</p>
By {{ blog_post.author.name }}
(Post #{{ blog_post.author.blog_posts|length }})
<p>{{ blog_post.text }}</p>
Database schema:
NONE
URL configuration file:
NONE
Other example applications: