wintermeyer

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

zachdaniel

Creator of Ash

Sorry, as: "product" :slight_smile:

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

wintermeyer

For the archive: Here’s the source code for a potential solution:

barnabasJ

barnabasJ

Ash Core Team

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

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.

Where Next?

Popular in Questions Top

New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement