wintermeyer

wintermeyer

I have a minimal Ash, PostgreSQL and Phoenix setup in which I do a

mix phx.gen.html Shop Product products name:string price:float --no-context

Obviously the controller doesn’t work out of the box. With some minor changes it works for
index, show and delete.

lib/app_web/controllers/product_controller.ex:

defmodule AppWeb.ProductController do
  use AppWeb, :controller

  # alias App.Shop
  alias App.Shop.Product

  def index(conn, _params) do
    products = Product.read!()
    render(conn, :index, products: products)
  end

  # def new(conn, _params) do
  #   changeset = Shop.change_product(%Product{})
  #   render(conn, :new, changeset: changeset)
  # end

  # def create(conn, %{"product" => product_params}) do
  #   case Shop.create_product(product_params) do
  #     {:ok, product} ->
  #       conn
  #       |> put_flash(:info, "Product created successfully.")
  #       |> redirect(to: ~p"/products/#{product}")

  #     {:error, %Ecto.Changeset{} = changeset} ->
  #       render(conn, :new, changeset: changeset)
  #   end
  # end

  def show(conn, %{"id" => id}) do
    product = Product.by_id!(id)
    render(conn, :show, product: product)
  end

  # def edit(conn, %{"id" => id}) do
  #   product = Shop.get_product!(id)
  #   changeset = Shop.change_product(product)
  #   render(conn, :edit, product: product, changeset: changeset)
  # end

  # def update(conn, %{"id" => id, "product" => product_params}) do
  #  product = Product.by_id!(id)
  #
  # case Product.update(product, product_params) do
  #  {:ok, product} ->
  #     conn
  #     |> put_flash(:info, "Product updated successfully.")
  #     |> redirect(to: ~p"/products/#{product}")

  #   {:error, %Ecto.Changeset{} = changeset} ->
  #     render(conn, :edit, product: product, changeset: changeset)
  # end
  #end

  def delete(conn, %{"id" => id}) do
    product = Product.by_id!(id)
    :ok = Product.destroy(product)

    conn
    |> put_flash(:info, "Product deleted successfully.")
    |> redirect(to: ~p"/products")
  end
end

Is it possible to change the other functions (how?) so that I don’t have to change the forms or do I always have to change the forms too?

For context:

Showing Posts 1 to 10

barnabasJ

barnabasJ

Ash Core Team

ash_phoenix has mix ash_phoenix.gen.live. I think this might be what you are looking for.

wintermeyer

wintermeyer OP

I would like to know if this can be “fixed” in the controller only or if the views have to be changed too.

barnabasJ

barnabasJ

Ash Core Team

Oh, I misunderstood. As long as you return the same data as before, it will work. I only use the API part of Phoenix, so I’m unfamiliar with this layer. Still, I’m guessing from the code that I see here that you will probably run into problems with the changeset because the HTML generated from Phoenix probably expects normal Ecto Changesets, and you would have to do a lot yourself to handle all of this.

zachdaniel

zachdaniel

Creator of Ash

Yep. You would need to switch to use AshPhoenix.Form.

zachdaniel

zachdaniel

Creator of Ash

Something like:

def edit(conn, %{"id" => id}) do
  product = Shop.get_product!(id)
  form = AshPhoenix.Form.for_update(product, :update, actor: ..., api: YourApi)
  render(conn, :edit, product: product, form: form)
end

def update(conn, %{"id" => id, "product" => product_params}) do
  product = Product.by_id!(id)

  product
  |> AshPhoenix.Form.for_update(:update, actor: ..., api: YourApi)
  |> AshPhoenix.Form.submit()
  |> case do
    {:ok, product} ->
      conn
      |> put_flash(:info, "Product updated successfully.")
      |> redirect(to: ~p"/products/#{product}")

    {:error, form} ->
      render(conn, :edit, product: product, form: form)
  end
end
wintermeyer

wintermeyer OP

How would the template look like?

Given:

  def edit(conn, %{"id" => id}) do
    product = Product.by_id!(id)
    form = AshPhoenix.Form.for_update(product, :update, actor: nil, api: App.Shop)
    render(conn, :edit, product: product, form: form)
  end

I tried this for edit.html.eex

<.header>
  Edit Product <%= @product.id %>
  <:subtitle>Use this form to manage product records in your database.</:subtitle>
</.header>

<.simple_form for={@form}>
  <.input type="name" label="Name" field={@form[:name]} />
  <.input type="price" label="Price" field={@form[:price]} />
  <:actions>
    <.button>Save</.button>
  </:actions>
</.simple_form>

<.back navigate={~p"/products"}>Back to products</.back>

Which results into this error:

function AshPhoenix.Form.fetch/2 is undefined (AshPhoenix.Form does not implement 
the Access behaviour. If you are using get_in/put_in/update_in, you can specify the 
field to be accessed using Access.key!/1)
zachdaniel

zachdaniel

Creator of Ash

Ah right you need to call to_form on the ash form in newer phoenix versions

wintermeyer

wintermeyer OP

I can’t find to_form in the documentation and don’t know how to use it.
<.simple_form to_form={@form}> doesn’t work. How do I have to add it?

zachdaniel

zachdaniel

Creator of Ash

Like AshPhoenix.Form.for_create(…) |> to_form() this is what you have to do in live view at least if you got the same error there.

Come to think of it, the issue might actually be this:


<.simple_form :let={form} for={@form}>
  <.input type="name" label="Name" field={form[:name]} />
  <.input type="price" label="Price" field={form[:price]} />
  <:actions>
    <.button>Save</.button>
  </:actions>
</.simple_form>

You should be using the phoenix form when accessing the field.

wintermeyer

wintermeyer OP

That leads to a problem with the update function. Here’s the code:

The controller

  def edit(conn, %{"id" => id}) do
    product = Product.by_id!(id)

    form =
      AshPhoenix.Form.for_update(product, :update, actor: nil, api: App.Shop)

    render(conn, :edit, product: product, form: form)
  end

  def update(conn, %{"id" => id, "product" => product_params}) do
    product = Product.by_id!(id)

    case Product.update(product, product_params) do
      {:ok, product} ->
        conn
        |> put_flash(:info, "Product updated successfully.")
        |> redirect(to: ~p"/products/#{product}")

      {:error, %Ecto.Changeset{} = changeset} ->
        render(conn, :edit, product: product, changeset: changeset)
    end
  end

The template

<.header>
  Edit Product <%= @product.id %>
  <:subtitle>Use this form to manage product records in your database.</:subtitle>
</.header>

<.simple_form :let={f} for={@form} action={~p"/products/#{@product}"}>
  <.input field={f[:name]} type="text" label="Name" />
  <.input field={f[:price]} type="number" label="Price" step="any" />
  <:actions>
    <.button>Save Product</.button>
  </:actions>
</.simple_form>

<.back navigate={~p"/products"}>Back to products</.back>

The form is displayed. When I click on “Save Product” I get this error:
no function clause matching in AppWeb.ProductController.update/2

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews