From cb961a63fb1b325def1ade7548dd0e1d08a460c3 Mon Sep 17 00:00:00 2001 From: devvit Date: Sun, 4 Mar 2018 23:14:54 +0800 Subject: [PATCH] multiple uploads example --- README.md | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/README.md b/README.md index 57598af..7cdb042 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,52 @@ Both public and signed urls will include the timestamp for cache busting, and ar MyApp.Avatar.url({user.avatar, user}, :thumb, signed: true) ``` +### Example of multiple uploads + +Multiple uploads through embedded schema. + +```elixir + +# Embeded schema +defmodule Photo do + use Ecto.Schema + use Arc.Ecto.Schema + + import Ecto.Changeset + + @primary_key false + + embedded_schema do + field :fileinfo, Myapp.Uploader.Type + end + + def changeset(photo, params \\ %{}) do + photo + |> cast(params, []) + |> cast_attachments(params, [:fileinfo], allow_paths: true) + end +end + +# Database schema +defmodule Post do + embeds_many :photos, Photo, on_replace: :delete +end + +# Controller +def update(conn, params) do + post = Repo.get! Post, params["id"] + + photos = for f <- params["uploads"] do + %Photo{} + |> Photo.changeset(%{fileinfo: f}) + end + + Post.changeset(post) + |> Ecto.Changeset.put_embed(:photos, photos) + |> Repo.update +end +``` + ## License Copyright 2015 Sean Stavropoulos