Search code examples
ruby-on-railstrestle-admin

Rails - using Trestle/admin to display associated records


I am using trestle for admin in a Rails project.

I have Teachers with many Students. There is a proper has_many and belongs_to association. I have everything working nicely in tabs, In one tab I have teacher information and in the other tab I want to display all their students. I have tried coding by guessing because there are no docs, and I have gotten nowhere. But here.

Trestle.resource(:teachers) do
...
  form do |teacher|
    tab :general do
      text_field :name  
      ...(this all works fine)
    end
    tab :students do
      (can't get anything working)
    end   
... 

Thanks for any suggestions.


Solution

  • You can do it like this:

    tab :tasks 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
    

    And in Your example it would be something of the sort:

    Trestle.resource(:teachers) do
    ...
      form do |teacher|
        tab :general do
          text_field :name  
          ...(this all works fine)
        end
        tab :students do
          table teacher.students, admin: :students do
            column :name, align: :center
            ...(your student object fields)
          end
    
          concat admin_link_to('New Student/s', admin: :students, action: :new, params: { teacher_id: teacher }, class: 'btn btn-success')
        end   
    ...