Skip to content

How To: Link To Trestle

coezbek edited this page Dec 7, 2021 · 8 revisions

Discover the routes

You can link to Trestle from other areas of your application. Use rails routes to see what's available under the Trestle::Engine:

Routes for Trestle::Engine:
warheads_admin_index GET    /warheads(.:format)          warheads_admin/admin#index
                     POST   /warheads(.:format)          warheads_admin/admin#create
  new_warheads_admin GET    /warheads/new(.:format)      warheads_admin/admin#new
 edit_warheads_admin GET    /warheads/:id/edit(.:format) warheads_admin/admin#edit
      warheads_admin GET    /warheads/:id(.:format)      warheads_admin/admin#show
                     PATCH  /warheads/:id(.:format)      warheads_admin/admin#update
                     PUT    /warheads/:id(.:format)      warheads_admin/admin#update
                     DELETE /warheads/:id(.:format)      warheads_admin/admin#destroy

Link using the automatic trestle url helper

Then from within your application add the trestle prefix to your routes:

%p= link_to 'Build a New Warhead for Fun and Profit!', trestle.new_warheads_admin_path

If you are outside a view context (views, controllers) you will need to use one of the two other methods.

Use trestle admin and resource class methods

Assuming you have declared Trestle.resource(:warhead) then the admin class will be WarheadsAdmin (calling Trestle.lookup(:warhead) will also return the relevant admin), and you can call the following methods:

WarheadsAdmin.path => "/admin/warhead"
WarheadsAdmin.path(:show, id: 123) => "/admin/warheads/123"

WarheadsAdmin.instance_path(warhead) => "/admin/warheads/1"
WarheadsAdmin.instance_path(warhead, action: :edit) => "/admin/warheads/1/edit"

Note that the instance_path method is only available on a Resource (declared with Trestle.resource(...)) and not a plain Admin (declared with Trestle.admin(...)).

Include the full url_helpers

Alternatively, you can include Trestle::Engine.routes.url_helpers in your class (e.g. active job) to include the Rails route helpers directly, e.g.

class PrintWarheadAdminPath < ActiveJob::Base
  include Trestle::Engine.routes.url_helpers
  
  def perform(warhead)
     puts warheads_admin_path(warhead)
  end
end

Link from Trestle back to your application

If you want to link to your application from Trestle, use the main_app prefix:

%p= link_to 'Back to the Command Center', main_app.root_path