2014-10-20 69 views
0

我有一個網站運行在中間人。我試圖檢查圖像文件是否存在。我遵循「How to check for file existence」中的建議,使用File.file?,但它不起作用。下面是代碼行:檢查圖像文件是否存在使用中間人

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

我嘗試了一些不同的文件路徑:

  • /來源/圖片/文件
  • /圖片/文件
  • ../images/file

但沒有工作。在瀏覽器中,我可以在「0.0.0.0:4567/images/file」查看圖像文件。

任何想法?

+0

嘗試'「../源/圖片/醫生 - #{} doctor.slug .JPG」' – Brennan 2014-10-20 21:38:46

+1

你混淆的文件路徑爲服務器將其報告給瀏覽器,以及解釋器和腳本在查看磁盤時如何看到它。這兩者非常不同。 – 2014-10-20 21:56:44

+0

我試過「../source/images/doctor-#{doctor.slug}.jpg」,它也沒有工作。微克。 – 2014-10-21 16:51:44

回答

2

也許通過Middleman Sitemap查詢這些文件比較容易嗎?

您可以通過sitemap對象訪問模板。實現你的實際情況的一種方法是:

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