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 ![]()
Most Liked
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
2
ambareesha7
Thank you, it’s opening now
2
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 ![]()
1
Popular in Questions
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
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
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
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
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
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lists...
New
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
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
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
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
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
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
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
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
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
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
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
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
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
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









