2012-03-02 79 views
3

我是一個rails新手。有人可以告訴我是否有一種快速簡便的方法來生成rtf文件供人們使用rails進行下載? 例如,如果我有「views/users/show.html.erb」視圖通常輸出到HTML,所以我想讓人們能夠下載爲一個相同的RTF文件?在Rails 3.2中生成rtf文件

+0

發現這個gem https:/ /github.com/thechrisoshow/rtf – 2012-03-02 20:13:46

回答

7

ruby-rtf是你正在尋找的寶石。有RTF代的一些例子here

一下添加到初始化/ mime_types.rb:

Mime::Type.register "text/richtext", :rtf 

代碼給你一個想法:

document = RTF::Document.new(RTF::Font.new(RTF::Font::ROMAN, 'Times New Roman')) 
document.paragraph do |p| 
    p << "This is the first sentence in the paragraph. " 
    p << "This is the second sentence in the paragraph. " 
    p << "And this is the third sentence in the paragraph." 
end 

send_file document, :type=>"text/richtext" 
+1

鏈接應該是https://rubygems.org/gems/ruby-rtf – 2013-02-18 09:06:57

+0

修復了鏈接。謝謝@AlexGhiculescu – ScottJShea 2013-02-18 17:44:46