Search code examples
elixirphoenix-frameworkecto

no case clause matching: :empty error in changeset for cast_attachments for file upload


I have the following model and for images I am using Arc module for upload

defmodule Chemical.Info do use Chemical.Web, :model

schema "infos" do
    field :title, :string
    field :shortdesc, :string
    field :longdesc, :string
    field :images, Chemical.ImageUploader.Type
    field :regions, :string
    field :startdate, :date
    field :enddate, :date
    field :status, :string
    field :createdby, :string
    field :approvedby, :string

    timestamps()
end

@required_fields ~w(title shortdesc startdate enddate )
@optional_fields ~w(longdesc regions status createdby approvedby)
@required_file_fields ~w()
@optional_file_fields ~w(images)

   def changeset(model, params \\ :empty) do
    model

    |> cast(params, @required_fields, @optional_fields)
    |> cast_attachments(params, @required_file_fields, @optional_file_fields )

end 

end

new function in controller

def new(conn, _params) do
    changeset = Info.changeset(%Info{})
    render conn, "new.html", changeset: changeset
end

I am getting the following error in cast_attachments function call when accessing the /new action which returns new form

no case clause matching: :empty

If I remove the cast_attachments line, it shows the new form. Arc version is 0.6.0 and arc_ecto is 0.5.0. I am using local storage so the default uploader generated by Arc.


Solution

  • Since Ecto 2.0, passing :empty as params is deprecated in favor of an empty map (%{}). It looks like arc_ecto 0.5.0 doesn't allow sending :empty anymore already instead of issuing warning. You need to change :empty to %{} as the default value of params:

    def changeset(model, params \\ :empty) do
    

    ->

    def changeset(model, params \\ %{}) do