wintermeyer
How to use phx.gen.html in an Ash setup?
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:
Marked As Solved
zachdaniel
Sorry, as: "product" ![]()
You don’t have to change anything in the form, you’d just be able to use "product" in your param match instead of "form"
i.e
i.e
```elixir
def update(conn, %{"product" => product_params, "id" => id}) do
Also Liked
wintermeyer
For the archive: Here’s the source code for a potential solution:
barnabasJ
ash_phoenix has mix ash_phoenix.gen.live. I think this might be what you are looking for.
zachdaniel
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









