       collection_action = Keyword.get(collection_actions, action) ->
         collection_action.(conn, params)
       true ->
-        apply(__MODULE__, action, [conn, params])
+        apply(__MODULE__, action, [conn, defn, params])
     end
   end
 

    handle_action(conn, action, conn.params["resource"])
  end

  defp handle_action(conn, action, nil) do
    ExAdmin.get_all_registered
    |> Enum.sort(&(elem(&1,1).menu[:priority] < elem(&2,1).menu[:priority]))
    |> hd
    |> case do
      {_, %{controller_route: resource}} ->
        conn = scrub_params(conn, resource, action)
        params = filter_params(conn.params)
        conn
        |> struct(path_info: conn.path_info ++ [resource])
        |> struct(params: Map.put(conn.params, "resource", resource))
        |> handle_action(action, resource)
      _other ->
        throw :invalid_route
    end
  end
  defp handle_action(conn, action, resource) do
    conn = scrub_params(conn, resource, action)
    params = filter_params(conn.params)

    end
  end

  def create(conn, defn, params) do
    model = defn.__struct__
    resource = model.__struct__.resource_model.__struct__
    resource_model = model.__struct__.resource_model
    |> base_name |> String.downcase |> String.to_atom
    changeset_fn = Keyword.get(defn.changesets, :create, &resource.__struct__.changeset/2)
    changeset = ExAdmin.Repo.changeset(changeset_fn, resource, params[resource_model])

    case ExAdmin.Repo.insert(changeset) do
      {:error, changeset} ->
        conn = put_flash(conn, :inline_error, changeset.errors)
        contents = do_form_view model, conn, changeset.data, params
        conn |> render("admin.html", html: contents, resource: resource, filters: nil, defn: defn)
      resource ->
        put_flash(conn, :notice, "#{base_name model} was successfully created.")
        |> redirect(to: get_route_path(resource, :show, Schema.get_id(resource)))
    end
  end

  def update(conn, defn, params) do
    model = defn.__struct__
    resource_model = model.__struct__.resource_model
    |> base_name |> String.downcase |> String.to_atom
    resource = model.run_query(repo, defn, :edit, params[:id])

    if resource == nil do
      raise Phoenix.Router.NoRouteError, conn: conn, router: __MODULE__
    end

    changeset_fn = Keyword.get(defn.changesets, :update, &resource.__struct__.changeset/2)
    changeset = ExAdmin.Repo.changeset(changeset_fn, resource, params[resource_model])
    case ExAdmin.Repo.update(changeset) do
      {:error, changeset} ->
        conn = put_flash(conn, :inline_error, changeset.errors)
        contents = do_form_view model, conn, changeset.data, params
        conn |> render("admin.html", html: contents, resource: resource, filters: nil, defn: defn)
      resource ->
        put_flash(conn, :notice, "#{base_name model} was successfully updated")
        |> redirect(to: get_route_path(resource, :show, Schema.get_id(resource)))
    end
  end

  def toggle_attr(conn, defn, %{attr_name: attr_name, attr_value: attr_value} = params) do
    resource = repo.get!(defn.resource_model, params[:id])
    attr_name_atom = String.to_existing_atom(attr_name)

