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
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
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function:
-dialyzer...
New
Lets say I have map like this fetching from my database
%{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Other popular topics
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
Hello guys,
I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
I would like to know what is the best IDE for elixir development?
New
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
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
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










