Skip to content

Instantly share code, notes, and snippets.

View BrandonMathis's full-sized avatar
🌸
me like code

Brandon Mathis BrandonMathis

🌸
me like code
View GitHub Profile
plug Plug.Static,
at: "/uploads", from: Path.expand('./uploads'), gzip: false
<!-- ... -->
<li>
<strong>Avatar:</strong>
<img src="<%= MyApp.Avatar.url({@user.avatar, @user}) %>"/>
</li>
<!-- ... -->
<!-- ... -->
<%= for user <- @users do %>
<tr>
<td><img src="<%= MyApp.Avatar.url({user.avatar, user}) %>"/></td>
<td><%= user.username %></td>
<td><%= user.email %></td>
<td class="text-right">
<%= link "Show", to: user_path(@conn, :show, user), class: "btn btn-default btn-xs" %>
<%= link "Edit", to: user_path(@conn, :edit, user), class: "btn btn-default btn-xs" %>
user = Repo.get(User, 1)
# To receive a single rendition:
MyApp.Avatar.url({user.avatar, user}, :thumb)
#=> "https://bucket.s3.amazonaws.com/uploads/avatars/1/thumb.png?v=63601457477"
# To receive all renditions:
MyApp.Avatar.urls({user.avatar, user})
#=> %{original: "https://.../original.png?v=1234", thumb: "https://.../thumb.png?v=1234"}
<%= form_for @changeset, @action, [multipart: true], fn f -> %>
<%= if @changeset.action do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
</div>
<% end %>
<div class="form-group">
<%= label f, :avatar, class: "control-label" %>
<%= file_input f, :avatar, class: "form-control" %>
#...
def create(conn, %{"user" => user_params}) do
changeset = User.changeset(%User{}, user_params)
case Repo.insert(changeset) do
{:ok, _user} ->
conn
|> put_flash(:info, "User created successfully.")
|> redirect(to: user_path(conn, :index))
{:error, changeset} ->
defmodule MyApp.User do
use MyApp.Web, :model
use Arc.Ecto.Model
schema "users" do
field :avatar, MyApp.Avatar.Type
field :username, :string
field :email, :string
timestamps
defmodule MyApp.Avatar do
use Arc.Definition
use Arc.Ecto.Definition
# ...
end
defp deps do
#...
{:arc_ecto, "~> 0.3.1"},
{:arc, "0.2.0"},
#...
end
scope "/", Roblist do
#...
resources "/users", UserController
#...
end