Skip to content

How To: Add Custom Action

Nathan Colgate edited this page Mar 30, 2018 · 7 revisions

Just like in the normal Rails framework, adding a custom action requires:

  • Adding a route
  • Adding an action to the controller
  • Linking to that action
Trestle.resource(:missiles) do
  # ...
  form do |missile|
    # ...
    sidebar do
      link_to('Launch!', admin.path(:launch, id: instance.id), method: :post, class: "btn btn-danger btn-block")
    end
  end

  controller do
    def launch
      missile = admin.find_instance(params)
      LaunchMissileJob.perform_later(missile_id: missile.id)
      flash[:message] = "Missile will be launched soon"
      redirect_to admin.path(:show, id: missile)
    end
  end

  routes do
    post :launch, on: :member
  end
end