2YY
2YY

Reputation: 229

How to render arc_ecto image in Phoenix framework template?

I written below code in my template.

<img src="<%= EdgeEc.Photo.url({@image.content, @image}, :original) %>" alt="" />

This is rendered like below.

<img src="uploads/large_myimagefilename.jpg.jpg?v=63615301283" alt="" />

Then, I typed the path localhost:4000/uploads/large_116.jpg.jpg?v=63615301283 in address bar and got below error.

no route found for GET /uploads/large_116.jpg.jpg (EdgeEc.Router)

Perhaps, because uploaded image is not model and it's not have routed controller.

Can I route uploaded images and rendering?

Upvotes: 2

Views: 1277

Answers (2)

t56k
t56k

Reputation: 6981

Or, in more detail, you want to serve static assets. I actually found that modifying the existing plug is the only thing that works, so, if your app name like in your example is EdgeEc:

In your lib/edge_ec/endpoint.ex:

plug Plug.Static,
  at: "/", from: :edge_ec,
  only: ~w(css fonts images js uploads favicon.ico robots.txt)

Also make sure your arc file module definition's storage directory starts with priv/static:

def storage_dir(_version, {_file, _scope}), do: "priv/static/uploads"

And write a helper for the URL generator to trim the priv/static:

def photo_url(struct) do
  Photo.url({struct.image, struct})
  |> Path.relative_to("priv/static")
end

Upvotes: 1

2YY
2YY

Reputation: 229

Self solved.

I found below issue.

https://github.com/stavro/arc_ecto/issues/4

Upvotes: 2

Related Questions