Skip to content
Add pages for a model

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 . migrate

Open 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:

TypeGoColumnInput
stringstringVARCHAR(255)a text box
textstringTEXTa text area
emailstringVARCHAR(255)an email box, checked with the email rule
intint64BIGINTa number box
floatfloat64doublea number box with decimals
boolboolbooleana checkbox
dateanetos.DateDATEa 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:date

What it writes

For Post:

FileHolds
app/models/post.goPost, with a field per column; anetos gen adds PostCols to models_gen.go
database/migrations/…_create_posts_table.goThe posts table
app/handlers/posts.goPosts: Index (20 a page, newest first), Show, New, Create, Edit, Update, Delete; PostInput, the form, with its validate tags
views/posts.templThe list, a post’s page, and the form for new and existing posts
routes/posts.goroutes.Posts: the seven routes, named posts.index, posts.show…
locales/en/posts.yamlThe pages’ text: titles, buttons, the fields’ labels, the messages
posts_test.goA 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.

RouteURLDoes
posts.indexGET /postsThe list, with pages (?page=2)
posts.newGET /posts/newThe empty form
posts.storePOST /postsCreates the post, then shows it
posts.showGET /posts/{id}The post, with Edit and Delete
posts.editGET /posts/{id}/editThe filled form
posts.updatePUT /posts/{id}Saves the changes
posts.destroyDELETE /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 the Posts(pages) call from routes/web.go to the members group of routes/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 to Post and PostInput, a line to fill, and the input to postFields in 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.