Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Detail pages

A detail page shows one record, read-only, at GET /admin/{slug}/{id}. A resource declares it with ResourceDef::detail: a Detail of the same columns a table lists, arranged in Section, Group, Grid and Tabs blocks, which the panel builds once when it mounts. Each column reads its value off the typed record, and declares the relations it reads:

#![allow(unused)]
fn main() {
impl Resource for PostResource {
    // …
    fn declare() -> ResourceDef<Self> {
        ResourceDef::new()
            // …
            .detail({
                let col = Post::columns();
                Detail::new(Section::new("Post").columns((
                    col.title,
                    col.body,
                    col.status,
                    RelationColumn::of::<AuthorResource>(relation!(Post.author)),
                )))
            })
    }
}
}

The page adds a View action to each row, and without_page(ResourcePage::View) turns it off. Mounting refuses a resource that serves the page with a detail listing no column. ResourceDef::detail says what the page’s header carries, what it loads and what it answers for a record the request may not see.

The default detail page

Without detail, the model’s PanelModel give the detail page, as they give the table: one column per field the table lists, plus its embedded values and lists, in declaration order. It reads the model, not the record form, so a field Toasty writes, such as a #[default] timestamp, shows too, and a NoForm resource has a detail page as well. The derive’s page lists the column each type gets and the fields it leaves out.

A resource that declares a table and no detail page shows the table’s columns there instead, in the table’s order, so a column declared once for the list, such as a RelationColumn or a formatted amount, reads the same on the record’s page.

A relation shows only through a column that includes it, as Author does above, and a file path shows as text until a FileColumn links it. A resource declares its own detail page to pick or arrange the columns:

#![allow(unused)]
fn main() {
pub fn audit_view() -> Detail<Audit> {
    let col = Audit::columns();
    Detail::new((col.action, col.created_at))
}
}

What the page shows

The header carries the record’s title, its actions, Delete and Edit, each as the policy allows them on this record. A resource that declares a record page or a relation tab adds the record’s navigation under the header. Below it come the detail’s columns, then the related tables.

Each column renders its label over its cell, as it renders in a table, never a control. A ComputedColumn shows anything the record determines, such as a reading time:

#![allow(unused)]
fn main() {
pub fn reading_time() -> ComputedColumn<Post> {
    ComputedColumn::new("Reading time", |_, post: &Post| {
        let words = post.body.split_whitespace().count();
        format!("{words} words, {} min", words.div_ceil(200))
    })
}
}

An app’s own Column renders its cell on the detail page too; Detail::column appends one, and an IntoDetail impl for its type places it in a block. A column that shows more than one value under one label overrides entry, which renders the label over the cell by default; EmbeddedColumn overrides it to give each leaf its own label.

Loading. The page loads only the relations its columns declare: a relation column declares its own, and a ComputedColumn declares one with include. A ComputedColumn reading one it does not declare finds it unloaded. A page of the app’s own renders a Detail the same way, with Detail::load and render.

Title. record_title names the text column that titles each record, here and wherever another resource points at it: each option of a relationship choice over the resource, and each cell of a RelationColumn::of it. A title built from several columns is a column the model stores; ResourceDef::record_title has the fallback and the rules:

#![allow(unused)]
fn main() {
            .record_title(lens!(Post.title))
}

Public link. public_link adds a link to the record’s public page to the header of its detail and edit pages, for each record it returns one for: the URL to link and the text to show.

#![allow(unused)]
fn main() {
            .public_link(|_cx: &Cx, post: &Post| {
                (post.status == PostStatus::Published)
                    .then(|| PublicLink::new(format!("/blog/{}", post.id), "View on the blog"))
            })
}

Record pages

A page of the app’s own about one record, such as an order’s history or a post’s search preview, is a RecordPage of the resource. The panel serves it at {list}/{key}/{slug} and loads the record the way the detail page does, through the resource’s query and policy and the request’s tenant. The page renders only its body, under the record’s heading:

#![allow(unused)]
fn main() {
/// How a search engine lists the post: `/admin/posts/{id}/search-preview`.
pub struct SearchPreviewPage;

impl RecordPage<PostResource> for SearchPreviewPage {
    fn declare() -> RecordPageDef<Post> {
        RecordPageDef::new().icon(tablo::ui::icons::SEARCH)
    }

    async fn render(cx: &Cx, post: Post) -> Result<impl View> {
        Ok(view! {
            <p class="text-lg text-primary">(post.seo.title)</p>
            <p class="text-sm text-muted-foreground">(post.seo.description)</p>
        })
    }
}
}

ResourceDef::record_page adds it to the resource, and the record’s navigation links it:

#![allow(unused)]
fn main() {
impl Resource for PostResource {
    // …
    fn declare() -> ResourceDef<Self> {
        ResourceDef::new()
            // …
            .record_page::<SearchPreviewPage>()
    }
}
}

RecordPageDef::access narrows who may open the page for a record, beyond those the policy lets view it. url::record_page builds a page’s URL for a record. The panel masks nothing the page renders: a page showing a field that field_access restricts asks can_view_field first. RecordPage has the checks a request passes and what render receives.

ResourceDef::relation adds a related resource whose rows belong to a record. Each renders on the record’s detail and edit pages as that resource’s own list table, narrowed to the record:

#![allow(unused)]
fn main() {
impl Resource for PostResource {
    // …
    fn declare() -> ResourceDef<Self> {
        ResourceDef::new()
            // …
            // The related model's foreign key, which holds the post's primary key.
            .relation(Relation::has_many::<CommentResource>(lens!(
                Comment.post_id
            )))
    }
}
}

The table is CommentResource’s own list table, its columns, search, sort, filters and pager included, narrowed to the post: the related resource keeps its policy, its actions and its create button, which opens with the post preselected. The panel must register the related resource too. Relation lists what the table carries, where its writes land, and how several tables share one page.

Relation tabs

A record with several related tables makes a long page, and its detail page loads every one. .tab() moves a relation’s table to a page of its own, at {list}/{key}/{related slug}: Relation::has_many::<CommentResource>(lens!(Comment.post_id)).tab() serves the post’s comments at /admin/posts/{id}/comments, and no longer under its detail and edit pages.

A resource with a tab or a record page gives its records a navigation: a tab bar under the header of the detail page, the edit page, each record page and each relation tab, linking those the request may open, the current one marked. The table on a tab is the one the detail page would show, with the same writes, landing back on the tab. Relation::tab has the checks and the mount refusals.

Many-to-many relations

Relation::belongs_to_many names the owner’s #[has_many(via = ..)] field over a join model, declared as for a many-to-many field. The table lists the related records the owner links, and attaches and detaches them as join rows:

#![allow(unused)]
fn main() {
impl Resource for TagResource {
    // …
    fn declare() -> ResourceDef<Self> {
        ResourceDef::new()
            // …
            // The tag's `via` field: its articles, which its page attaches and detaches.
            .relation(Relation::belongs_to_many::<ArticleResource>(
                Tag::fields().articles(),
            ))
    }
}
}

The section’s header carries Attach, and each row and the bulk bar carry Detach, which deletes the join rows and leaves the records. Both ask the owner’s policy for Update, since linking a record edits the owner. The rows keep the related resource’s View, Edit and Delete, but no custom action and no create button: a record created there would link nothing. Relation::belongs_to_many has the routes, the hooks and the mount checks.

Relations through other relations

A customer’s payments belong to its orders, not to the customer. Toasty reaches them with a #[has_many(via = orders.payments)] field on Customer, and Relation::through::<PaymentResource>(Customer::fields().payments()) lists them on the customer’s page. The table is PaymentResource’s own, with its policy and actions, and it offers no create button and no Attach: no column of a payment holds the customer, so the table only reads. Relation::through has the rules.

One child per record

A record’s one child, such as a user’s profile, takes no table. Relation::has_one::<ProfileResource>(lens!(Profile.user_id)) renders ProfileResource’s own form inside the user’s create and edit form instead, and the save writes both in one transaction. The profile goes through its own resource: its policy, field access, rules and hooks apply as on its own pages, and the user’s key fills its foreign key. Relation::has_one has the rules and the mount checks.