Add pages for a model
Since v0.3.0
make:crud writes a model with its table, and the pages to list, show,
create, edit and delete its rows, styled by the starter theme, with
their routes and a test. You get working pages in a minute, and code
you can read and change.
Generate them
go tool anetos make:crud Post title:string body:text published:bool
go run . migrateOpen http://localhost:8080/posts (the header links to it): an empty list with a “New post” button. Save the form empty to see the validation messages, then fill it in.
Each field is name:type, then :optional or :unique if you like:
| Type | Go | Column | Input |
|---|---|---|---|
string | string | VARCHAR(255) | a text box |
text | string | TEXT | a text area |
email | string | VARCHAR(255) | an email box, checked with the email rule |
int | int64 | BIGINT | a number box |
float | float64 | double | a number box with decimals |
bool | bool | boolean | a checkbox |
date | anetos.Date | DATE | a date picker |
Strings, texts, emails and dates are required; :optional lets them be
empty. :unique, for required strings and emails and for dates, adds a
unique index and the unique rule, so the form says “The email has
already been taken.” rather than failing. (An optional string is empty,
not NULL, when left blank, and a blank number is 0: only one row could
have either, so make:crud refuses them; add such an index by hand.)
go tool anetos make:crud Customer name:string email:email:unique notes:text:optional since:dateWhat it writes
For Post:
| File | Holds |
|---|---|
app/models/post.go | Post, with a field per column; anetos gen adds PostCols to models_gen.go |
database/migrations/…_create_posts_table.go | The posts table |
app/handlers/posts.go | Posts: Index (20 a page, newest first), Show, New, Create, Edit, Update, Delete; PostInput, the form, with its validate tags |
views/posts.templ | The list, a post’s page, and the form for new and existing posts |
routes/posts.go | routes.Posts: the seven routes, named posts.index, posts.show… |
locales/en/posts.yaml | The pages’ text: titles, buttons, the fields’ labels, the messages |
posts_test.go | A test that creates, shows, edits and deletes a post through the pages |
It also adds Posts(pages) to routes/web.go (the page group, with
sessions and CSRF protection) and a link to the list in the layout’s
header. It never overwrites a file: if one exists, it writes nothing.
| Route | URL | Does |
|---|---|---|
posts.index | GET /posts | The list, with pages (?page=2) |
posts.new | GET /posts/new | The empty form |
posts.store | POST /posts | Creates the post, then shows it |
posts.show | GET /posts/{id} | The post, with Edit and Delete |
posts.edit | GET /posts/{id}/edit | The filled form |
posts.update | PUT /posts/{id} | Saves the changes |
posts.destroy | DELETE /posts/{id} | Deletes the post, then lists the others |
HTML forms send PUT and DELETE with a hidden _method field, which
the project’s web.MethodOverride reads.
Make it yours
The code is plain Anetos code: change it as you would your own.
- Only for signed-in users. After
make:auth, move thePosts(pages)call fromroutes/web.goto themembersgroup ofroutes/auth.go(Posts(members)): guests then go to the login page. - Another column. Add a migration (
go tool anetos make:migration add_slug_to_posts_table), the field toPostandPostInput, a line tofill, and the input topostFieldsin the view. - Other text. Change
locales/en/posts.yaml; add a language with its own folder (Translations). - Look. The pages use the starter theme’s classes (Style your app).
go test ./... runs the test it wrote; keep it passing as you change
the pages.
When to write pages by hand
make:crud makes the common case quick: a table, a form, plain
validation. Pages with relations, permissions or htmx start from its
code, or from scratch: the tutorial builds issue
pages by hand, step by step, and the guides cover
forms, validation,
queries and relations.
Next: Add accounts.