【问题标题】:Check if image file exists using middleman使用中间人检查图像文件是否存在
【发布时间】:2014-10-20 21:33:45
【问题描述】:

我有一个在中间人上运行的网站。我正在尝试检查图像文件是否存在。我遵循“How to check for file existence”中的建议,使用File.file?,但它不起作用。这是代码行:

<% if File.exists?("/source/images/doctor-#{doctor.slug}.jpg") %>

我尝试了几种不同的文件路径:

  • /source/images/file
  • /图像/文件
  • ../images/file

但没有一个有效。在浏览器中,我可以在“0.0.0.0:4567/images/file”查看图像文件。

有什么想法吗?

【问题讨论】:

  • 试试"../source/images/doctor-#{doctor.slug}.jpg"
  • 当服务器将文件报告给浏览器时,您会混淆文件的路径,而不是解释器和脚本在查看磁盘时如何看到它。两者非常不同。
  • 我试过“../source/images/doctor-#{doctor.slug}.jpg”,但也没有用。呃。
  • 试试"./source/images/doctor-#{doctor.slug}.jpg"

标签: ruby image file middleman


【解决方案1】:

也许通过Middleman Sitemap查询这些文件更容易?

您可以通过sitemap 对象在模板中访问它。实现您的实际案例的一种方法可能是:

<% if sitemap.find_resource_by_path("images/doctor-#{doctor.slug}.jpg") %>
    <%# do what you want %>
<% end %>

【讨论】:

    猜你喜欢
    • 2013-01-13
    • 2016-01-27
    • 2011-02-25
    • 2013-07-18
    • 1970-01-01
    • 2015-09-10
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多