Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display all fields in a form when adding a new tab #346

Closed
rahulkgupta opened this issue Feb 25, 2021 · 5 comments
Closed

Display all fields in a form when adding a new tab #346

rahulkgupta opened this issue Feb 25, 2021 · 5 comments

Comments

@rahulkgupta
Copy link

By default, all the fields in a model show up in a Trestle form, which is great. I was trying to add a tab by following https://github.com/TrestleAdmin/trestle/wiki/Has-Many-Relationship-Tab in order to display subresource but ran into a problem where I need to then manually add all the fields in the form again. Is there a simple way where I can add all the fields without needing to enumerate through each one?

@spohlenz
Copy link
Member

spohlenz commented Mar 1, 2021

I gather that rather than needing to manually add all of the fields to the form, you actually mean the table? Since the form in the Has-Many example still lives on the child resource (tasks in that example).

Assuming that is correct, you can reference an existing table from another resource's form. From that wiki page you have the example code:

Trestle.resource(:projects) do
  form do |project|
    tab :project do
      text_field :name
      text_area :description
    end

    tab :tasks, badge: project.tasks.size do
      table project.tasks, admin: :tasks do
        column :description, link: true
        column :done, align: :center
        column :created_at, align: :center
        actions
      end

      concat admin_link_to("New Task", admin: :tasks, action: :new, params: { project_id: project }, class: "btn btn-success")
    end
  end
end

However you might already have a top-level resource and table defined for all tasks:

Trestle.resource(:tasks) do
  table do
    column :description, link: true
    column :done, align: :center
    column :created_at, align: :center
    actions
  end
end

The original code can then become:

Trestle.resource(:projects) do
  form do |project|
    tab :project do
      text_field :name
      text_area :description
    end

    tab :tasks, badge: project.tasks.size do
      table TasksAdmin.tables[:index], collection: project.tasks
      concat admin_link_to("New Task", admin: :tasks, action: :new, params: { project_id: project }, class: "btn btn-success")
    end
  end
end

In many cases, your top-level table might include additional columns that you don't want to show on the sub-resource. In this you can define additional name tables on the top-level resource and reference them from the child resource.

Trestle.resource(:tasks) do
  # Default table (:index)
  table do
    column :project
    column :description, link: true
    column :done, align: :center
    column :created_at, align: :center
    actions
  end

  # Named table
  table :project do
    column :description, link: true
    column :done, align: :center
    column :created_at, align: :center
    actions
  end
end

This is a pattern I use in many of my own projects. Eventually I'd like a way to avoid the repetition that this still brings but it at least keeps all of the table definitions in the one place.

@rahulkgupta
Copy link
Author

O that's super helpful. that will help us DRY things up. Using your example, I was actually wondering about project. By default, the form fields are populated. However, when I introduce a tasks tab, I have to manually add text_field :name and a field for every attribute of project.

@spohlenz
Copy link
Member

spohlenz commented Mar 3, 2021

Ah, I think I understand you now.

The default (automatic) form fields can be added within a tab using the following code:

Trestle.resource(:projects) do
  form do |project|
    tab :project do
      Trestle::Form::Automatic.new(admin).render(self, project)
    end
  end
end

@rahulkgupta
Copy link
Author

O thanks! Was this documented somewhere that I missed?

@spohlenz
Copy link
Member

spohlenz commented Mar 3, 2021

No, you haven't missed anything. To be honest, this solution does use some internal APIs that aren't necessarily public. However I don't expect them to change anytime soon.

The good news is that I have started putting more work into the docs again as they have been lacking for far too long.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants