[英]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. 然后,我在地址栏中输入路径
localhost:4000/uploads/large_116.jpg.jpg?v=63615301283
,并得到以下错误。
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? 我可以路由上传的图像和渲染吗?
Self solved. 自我解决。
I found below issue. 我发现以下问题。
https://github.com/stavro/arc_ecto/issues/4 https://github.com/stavro/arc_ecto/issues/4
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: 我实际上发现修改现有
plug
是唯一可行的方法,因此,如果您的示例中的应用程序名称为EdgeEc,则:
In your lib/edge_ec/endpoint.ex
: 在您的
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
: 还要确保您的弧文件模块定义的存储目录以
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
: 并为URL生成器编写一个帮助程序以修剪
priv/static
:
def photo_url(struct) do
Photo.url({struct.image, struct})
|> Path.relative_to("priv/static")
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.