Skip to content

How to add custom actions

Maciej Bembenista edited this page Sep 14, 2018 · 3 revisions

The following illustrates how to customize actions, bypassing the default controller action for a given resource.

  1. Create a new controller with the given action
  2. Add a route for the action

See admin_resource_controller.ex for ExAdmin's implementation of the resource controller.

Example

# web/controllers/model_controller.ex
defmodule MyProject.Admin.ModelController do
  @resource "models"
  use ExAdmin.Web, :resource_controller

  def update(conn, defn, _params) do
    resource = conn.assigns.resource
    changeset = apply(defn.resource_model, defn.update_changeset, [resource, params[defn.resource_name]])
    # ...
  end
end
# web/router.ex
scope "/admin", MyProject.Admin do
  put "/models/:id", ModelController, :update
end