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:
https://elixir-phoenix-ash.com/ash/postgresql/phoenix.html#_phx_gen_html