Application.put_env(:sample, SamplePhoenix.Endpoint, http: [ip: {127, 0, 0, 1}, port: 5001], server: true, live_view: [signing_salt: "aaaaaaaa"], secret_key_base: String.duplicate("a", 64) ) Mix.install([ {:plug_cowboy, "~> 2.7"}, {:jason, "~> 1.0"}, {:phoenix, "1.7.12"}, {:phoenix_live_view, "0.20.14"}, {:ash, "3.0.0-rc.36"}, {:ash_phoenix, "2.0.0-rc.8"} ]) defmodule SamplePhoenix.ErrorView do def render(template, _), do: Phoenix.Controller.status_message_from_template(template) end defmodule NormalContent do use Ash.Resource, data_layer: :embedded attributes do attribute :body, :string, allow_nil?: false end actions do defaults [:read, create: [:body], update: [:body]] end end defmodule Content do use Ash.Type.NewType, subtype_of: :union, constraints: [ types: [ normal: [ type: NormalContent, tag: :type, tag_value: :normal ] ] ] end defmodule Post do use Ash.Resource, data_layer: Ash.DataLayer.Ets, domain: Domain attributes do uuid_primary_key :id attribute :title, :string, allow_nil?: false attribute :content, Content, allow_nil?: false end actions do defaults [create: [:title, :content], update: [:title, :content]] end end defmodule Domain do use Ash.Domain resources do resource Post end end defmodule SamplePhoenix.SampleLive do use Phoenix.LiveView, layout: {__MODULE__, :live} def mount(_params, _session, socket) do mode = "update" form = case mode do "create" -> Post |> AshPhoenix.Form.for_create(:create, forms: [auto?: true]) |> to_form() |> AshPhoenix.Form.add_form(:content, params: %{"type" => "normal"}) "update" -> post = Post |> Ash.Changeset.new() |> Ash.Changeset.change_attributes(%{ title: "title", content: %{"type" => "normal", "body" => "body"} }) |> Ash.create!() post |> AshPhoenix.Form.for_update(:update, forms: [auto?: true]) |> to_form() end socket = socket |> assign(form: form) |> assign(result: nil) {:ok, socket} end # layout def render("live.html", assigns) do ~H""" <%= @inner_content %> """ end # render def render(assigns) do ~H""" <.form id="form" for={@form} phx-change="validate" phx-submit="save">