Hi. I am a newbie in programming learning Elixir and Phoenix. I struggle with passing data from the edit form. I face error as in the title. Does anyone have any idea what I have done wrong? Adding a new product works but editing not.
Here are my schemas:
schema "categories" do
field :name, :string
field :subcategory, :string
has_many :products, Shopify.Inventory.Product
timestamps()
end
schema "products" do
field :avdate, :date
field :description, :string
field :name, :string
field :price, :float
field :prodimg, :string
field :quantity, :integer
field :subcategory, :integer
belongs_to :category, Shopify.Departments.Category
has_many :comments, Shopify.Reviews.Comment
timestamps()
And product controller:
def index(conn, _params) do
products = Inventory.list_products()
render(conn, "index.html", products: products)
end
def new(conn, _params) do
changeset = Inventory.change_product(%Product{})
categories = Repo.all(Category) |> Enum.map(&{&1.name, &1.id})
render(conn, "new.html", changeset: changeset, categories: categories)
end
def create(conn, %{"product" => product_params}) do
case Inventory.create_product(product_params) do
{:ok, product} ->
conn
|> put_flash(:info, "Product created successfully.")
|> redirect(to: Routes.product_path(conn, :show, product))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
def show(conn, %{"id" => id}) do
product = Inventory.get_product!(id)
comment_changeset = Reviews.change_comment(%Comment{})
render(conn, "show.html", product: product, comment_changeset: comment_changeset)
end
def edit(conn, %{"id" => id}) do
product = Inventory.get_product!(id)
changeset = Inventory.change_product(product)
categories = Repo.all(Category) |> Enum.map(&{&1.name, &1.id})
render(conn, "edit.html", product: product, categories: categories)
end
def update(conn, %{"id" => id, "product" => product_params}) do
product = Inventory.get_product!(id)
case Inventory.update_product(product, product_params) do
{:ok, product} ->
conn
|> put_flash(:info, "Product updated successfully.")
|> redirect(to: Routes.product_path(conn, :show, product))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "edit.html", product: product, changeset: changeset)
end
end
def delete(conn, %{"id" => id}) do
product = Inventory.get_product!(id)
{:ok, _product} = Inventory.delete_product(product)
conn
|> put_flash(:info, "Product deleted successfully.")
|> redirect(to: Routes.product_path(conn, :index))
end
end