Allyedge

Allyedge

Using storage_dir in Waffle and Waffle Ecto

# 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 :slightly_smiling_face:

Most Liked

akashv

akashv

Can you try once with removing the avatar from first cast? My working config is like below and everything else is similar, so I’m not sure what else could be the problem apart from the user being an empty map.

 def avatar_changeset(user, attrs) do
    user
    |> cast(attrs, [])
    |> cast_attachments(attrs, [:avatar])
    |> validate_required([:avatar])
  end
ambareesha7

ambareesha7

Thank you, it’s opening now

Allyedge

Allyedge

Hey, I did that and realised that my problem was in the image getting functions, not the ones that create it.

Everything works now :slightly_smiling_face:

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement