未定义函数 cast_attachments/3

undefined function cast_attachments/3

Elixir 和 Phoenix 的新手。尽我所能。
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

错误:

== 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

据我了解,您正在使用 arc_ecto 上传图片。

那么您可能希望 use Arc.Ecto.Schema 包含 cast_attachments 宏:

 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