2015-09-06 32 views
0

我試圖用diffy gem比較本地文件與互聯網文件。如何使用diffy gem顯示兩個文件之間的差異?

def show 
    require 'open-uri' 
    @file1 = File.open('app/assets/files/example.html') 
    @file2 = open('http://www.example.com/example.html') 
end 

在我看來,我有:

<%= puts Diffy::Diff.new(@file1, @file2).to_s(:html_simple) %> 

然而,這從我的菜單欄等(沒有錯誤信息或者)除了產生在我看來,沒有內容,即使我做了肯定的兩個文件確實不同。在可能/應該列出差異的地方,開發頁面的源代碼是空的。

我也試過<%= puts Diffy::Diff.new(@file1, @file2, :source => 'files')但產生的誤差no implicit conversion of File into String。我究竟做錯了什麼?

更新:服務器日誌顯示以下內容:

Started GET "/test_diffy" 
<div class="diff"> 
    <ul> 
    <li class="del"><del>#&lt;File:0x007f369032e038&gt;</del></li> 

    <li class="ins"><ins>#&lt;File:0x007f36902eab80&gt;</ins></li> 

    </ul> 
</div> 
    Rendered test_diffy/show.html.erb within layouts/application (2.7ms) 
    Rendered shared/_shim.html.erb (0.1ms) 
    Rendered layouts/_header.html.erb (45.2ms) 
    Organization Exists (0.4ms) SELECT 1 etc. 
    Rendered shared/_footer.html.erb (1.9ms) 
Completed 200 OK in 1136ms (Views: 386.7ms | ActiveRecord: 1.3ms) 

如果我放在debugger在Show View,它說爲@file1#<File:0x00000005cb5728>@file2類似。

UPDATE2:<%= puts Diffy::Diff.new(@file1, @file2) %>我也在我的看法與嘗試。該觀點仍然沒有表現出任何的差異,然後服務器日誌包括:

-#<File:0x007f3692bae4e0> 
\ No newline at end of file 
+#<File:0x007f3693065c18> 
\ No newline at end of file 

不知道這意味着什麼/怎麼辦!?

回答

1

快速瀏覽https://github.com/samg/diffy,初始值設定程序似乎期望字符串作爲參數傳遞。一種可能的解決辦法是將它們傳遞之前,把你投入的字符串:

file1_content = File.open('app/assets/files/example.html').read 
file2_content = open('http://www.example.com/example.html').read 
@diff = Diffy::Diff.new(file1_content, file2_content).to_s(:html_simple) 

然後在您的視圖與

<%= @diff %> 
+0

感謝使它,這個工作! Diffy還提供了比較文件:https://github.com/samg/diffy#diffing-files-instead-of-strings。但是,如果我嘗試使用@diff = Diffy :: Diff.new(file1_content,file2_content,:source =>'files'),它會爲#生成錯誤消息'undefined method'html_safe' (不管我是否刪除'.read')。如果我從視圖中移除'.html_safe',它確實顯示了視圖,但沒有顯示任何差異。 – Marty

+0

您是否嘗試過沒有':source =>'files''?在這一點上,它實際上只是一個文件的內容,而不是一個文件。也許渲染'<%= @diff %>'就夠了。 – panmari

+0

是的,謝謝,那麼它的作品。 – Marty

相關問題