【问题标题】:undefined function cast_attachments/3未定义函数 cast_attachments/3
【发布时间】:2019-01-12 12:58:58
【问题描述】:

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

错误:

== 文件 lib/countdown/posts/post.ex 中的编译错误 == ** (CompileError) lib/countdown/posts/post.ex:19: 未定义函数 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: Kernel.ParallelCompiler.spawn_workers/6 中的匿名 fn/4

【问题讨论】:

    标签: elixir image-uploading phoenix


    【解决方案1】:

    据我了解,您正在使用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
    

    【讨论】:

    • 然后我得到这个错误:lib/countdown/posts/post.ex:6: undefined function schema/2
    • 谢谢。它的工作。所以我们需要 Arc.Ecto 只包含 cast_attachment 方法。其余部分将从 Ecto.schema 中获取。我假设 Arc.Ecto.Schema 是 Ecto.schema 的超集
    • @RadzSingh 是的,我也是
    猜你喜欢
    • 2016-09-09
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多