I need to create a function, where I can insert the full path of my image in my database …
I do not intend to use any library. Ex: Arc
How do I insert this path into my database?
When entering the form information, my inspect returns like this:
test_params =
%{
"avatar" => %Plug.Upload{
content_type: "image/png",
filename: "test.png",
path: "/tmp/plug-1586/multipart-1586737577-25205180440993-4"
},
"companyname" => "Test",
"contactphone" => "00000000",
"firstname" => "test1",
"lastname" => "test2"
}
Could you help me create this role?
I am currently working on this function below.
def upload_route(conn, params) do
if upload = params["avatar"] do
user = Pow.Plug.current_user(conn)
System.cmd("mkdir", ["priv/static/upload_client/#{user.id}"])
File.cp(upload.path, "priv/static/upload_client/#{user.id}/#{upload.filename}") # your phoenix project dir
end
end
I thought about using Repo.insert and get the url with conn.host
controller, function create.
def create(conn, %{"test" => test_params}) do
IO.inspect test_params
changeset = Pow.Plug.current_user(conn)
|> Ecto.build_assoc(:test)
|> Profile.changeset(test_params)
TestController.upload_route(conn, test_params)
case Repo.insert(changeset) do
{:ok, profile} ->
conn
|> put_flash(:info, "Test criado com sucesso.")
|> redirect(to: Routes.test_path(conn, :show, test))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
end