Search code examples
file-uploadelixirphoenix-framework

undefined function cast_attachments/3


New to Elixir and Phoenix. Tried whatever I can do.
defmodule Countdown.Posts.Post do use Ecto.Schema import Ecto.Changeset schema "posts" do field :description, :string field :image, Countdown.PostUploader.Type field :shot, :naive_datetime field :title, :string timestamps() end @doc false def changeset(post, attrs) do post |> cast(attrs, [:title, :shot, :description, :image]) |> cast_attachments(params, [:image]) |> validate_required([:title, :shot, :description, :image]) end end

error:

== Compilation error in file lib/countdown/posts/post.ex == ** (CompileError) lib/countdown/posts/post.ex:19: undefined function cast_attachments/3 (stdlib) lists.erl:1338: :lists.foreach/2 (stdlib) erl_eval.erl:677: :erl_eval.do_apply/6 (elixir) lib/kernel/parallel_compiler.ex:198: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6


Solution

  • As far as I understand, you're using arc_ecto to upload an image.

    Then you might want to use Arc.Ecto.Schema to have the cast_attachments macro included:

     defmodule Countdown.Posts.Post do
       use Ecto.Schema
       use Arc.Ecto.Schema
       import Ecto.Changeset
    
        schema "posts" do
          field :description, :string
          field :image, Countdown.PostUploader.Type
          field :shot, :naive_datetime
          field :title, :string
          timestamps()
        end
    
        @doc false
        def changeset(post, attrs) do
          post
          |> cast(attrs, [:title, :shot, :description, :image])
          |> cast_attachments(params, [:image])
          |> validate_required([:title, :shot, :description, :image])
      end
    end