Skip to content

How to: Create a Singular Resource

coezbek edited this page Dec 6, 2021 · 1 revision

In case only a particular instance of a certain Model is to be shown in an admin view, you can use an instance block to identify the instance which is to be displayed.

For example if /admin/profile should link to the current_user instance of the User model do the following (observe that everything is the same except instance instead of collection is used):

Trestle.resource(:profile, singular: true, model: User) do

  instance { current_user } # To have a singleton model, use: User.first_or_create

  menu do
    item :profile, icon: "fa fa-star"
  end

  form do |user|
    text_field :name
    text_field :email
  end
end