# My AvatarUploader
# Override the storage directory:
def storage_dir(_version, {_file, scope}) do
"uploads/user/avatars/#{scope.id}"
end
# My User schema
use Waffle.Ecto.Schema
schema "users" do
field :avatar, Allychat.Uploaders.AvatarUploader.Type
field :email, :string
field :password, :string, virtual: true, redact: true
field :hashed_password, :string, redact: true
field :confirmed_at, :naive_datetime
timestamps()
end
def avatar_changeset(user, attrs) do
user
|> cast(attrs, [:avatar])
|> cast_attachments(attrs, [:avatar])
|> validate_required([:avatar])
end
# My Accounts context
def change_user_avatar(user, attrs \\ %{}) do
User.avatar_changeset(user, attrs)
end
def update_user_avatar(user, attrs) do
changeset =
user
|> User.avatar_changeset(attrs)
Ecto.Multi.new()
|> Ecto.Multi.update(:user, changeset)
|> Repo.transaction()
|> case do
{:ok, %{user: user}} -> {:ok, user}
{:error, :user, changeset, _} -> {:error, changeset}
end
end
When I do this, it tells me that it can’t find id
in nil
. I tried a lot of options, also the one from waffle_ecto
docs, and still couldn’t find an answer on how to upload an image to the storage_dir
.
Does anyone know what I can do to fix this? I’m pretty new to Waffle