2012-09-11 19 views
1

我是完全新的Rails,我試圖讓每個文件類型在一個目錄中有它自己的圖標,我只能讓它顯示一個請幫助?這是我到目前爲止。試圖讓每個文件類型都有它自己的圖標在軌道3.2

控制器:

class DocsController < ApplicationController 
def port 
    @files = Dir.glob("public/folder/*") 

    filetype = [".pdf", ".txt"] 

    if filetype.include? ".pdf" 
    @extension = "pdf.png" 
    elsif filetype.include? ".txt" 
    @extension = "text.png" 
    else 
    @extension = "folder.png" 
    end 
end 
end 

查看:

<% @files.each do |file| %> 
<div class="filediv"> 
    <%= image_tag @extension, :size => "150x150" %> 
    <p><%= file.gsub("public/folder/", "") %></p> 
</div> 
<% end %> 

這是導致一切具有PDF圖標,可有人告訴我,我做錯了什麼?

感謝

+0

如果filetype.include?('。pdf')總是返回true。你需要在文件上循環。 – tommasop

+0

謝謝,我怎麼可以重新寫它? – Ollie2619

回答

1

我認爲你需要添加的輔助方法

def extension_image(file) 
    ext =File.extname(file) 
    if ext==".pdf" 
    "pdf.png" 
    elsif ext == ".txt" 
    "text.png" 
    else 
    "folder.png" 
    end 
end 


<%= image_tag extension_image(file), :size => "150x150" %> 

文件類型= [ 「.PDF」, 「.TXT」]

刪除此代碼。

if filetype.include? ".pdf" 
    @extension = "pdf.png" 
    elsif filetype.include? ".txt" 
    @extension = "text.png" 
    else 
    @extension = "folder.png" 
    end 
end 

現在爲什麼總是顯示PDF擴展 很簡單

[1,2,3].include?(1) it's always true so no further checking 

Checkout的包括陣列的作品。

+0

謝謝阿馬爾,我不能投票,因爲我只是剛剛報名,但非常感謝,你能解釋ext = File.extname(文件)部分嗎? – Ollie2619

+0

我在Rails API上找到它了,再次感謝你! – Ollie2619

相關問題