Ruby on rails ';编码::未定义的转换器错误“\xE9“;从ASCII-8BIT到UTF-8';在RubyonRails中使用file.open的一些RubyGems时 我想要什么?
我想从Rails 3.2中的模板文件生成.docx文件或.odt文件Ruby on rails ';编码::未定义的转换器错误“\xE9“;从ASCII-8BIT到UTF-8';在RubyonRails中使用file.open的一些RubyGems时 我想要什么?,ruby-on-rails,ruby,ruby-on-rails-3.2,Ruby On Rails,Ruby,Ruby On Rails 3.2,我想从Rails 3.2中的模板文件生成.docx文件或.odt文件 我想在里面用日语。 在ubuntu服务器12.04和ruby 1.9.3p194以及rails 3.2.8中 发生了什么事? 我试过gems“docx templater”和“serenity” ruby docx模板器 1个样本效果良好 2尝试在我的rails应用程序中执行同样的操作 在控制器中作为示例 def gen_docx input_file = './app/template/ExampleTemplate.
我想在里面用日语。
在ubuntu服务器12.04和ruby 1.9.3p194以及rails 3.2.8中 发生了什么事? 我试过gems“docx templater”和“serenity”
ruby docx模板器
1个样本效果良好
2尝试在我的rails应用程序中执行同样的操作
在控制器中作为示例
def gen_docx
input_file = './app/template/ExampleTemplate.docx'
data = {
:teacher => "Priya Vora",
:building => "Building #14",
:classroom => :'Rm 202',
:district => "Washington County Public Schools",
:senority => 12.25,
:roster => [
{:name => 'Sally', :age => 12, :attendence => '100%'},
{:name => :Xiao, :age => 10, :attendence => '94%'},
{:name => 'Bryan', :age => 13, :attendence => '100%'},
{:name => 'Larry', :age => 11, :attendence => '90%'},
{:name => 'Kumar', :age => 12, :attendence => '76%'},
{:name => 'Amber', :age => 11, :attendence => '100%'},
{:name => 'Isaiah', :age => 12, :attendence => '89%'},
{:name => 'Omar', :age => 12, :attendence => '99%'},
{:name => 'Xi', :age => 11, :attendence => '20%'},
{:name => 'Noushin', :age => 12, :attendence => '100%'}
],
:event_reports => [
{:name => 'Science Museum Field Trip', :notes => 'PTA sponsored event. Spoke to Astronaut with HAM radio.'},
{:name => 'Wilderness Center Retreat', :notes => '2 days hiking for charity:water fundraiser, $10,200 raised.'}
],
:created_at => "11-12-03 02:01"
}
DocxTemplater::DocxCreator.new(input_file, data).generate_docx_file()
end
3但出现了错误在gem(docx_templater.rb 22)中的以下点引发的错误 宁静
1个示例运行良好
2在我的rails应用程序中也这样做,效果很好
像这样
#encoding: utf-8
require 'serenity'
class Showcase
include Serenity::Generator
Person = Struct.new(:name, :items)
Item = Struct.new(:name, :usage)
def generate_showcase
@title = 'Serenity inventory'
mals_items = [Item.new('Moses Brothers Self-Defense Engine Frontier Model B', 'Lock and load')]
mal = Person.new('Malcolm Reynolds', mals_items)
jaynes_items = [Item.new('Vera', 'Callahan full-bore auto-lock with a customized trigger, double cartridge and thorough gauge'),
Item.new('Lux', 'Ratatata'),
Item.new('Knife', 'Cut-throat')]
jayne = Person.new('Jayne Cobb', jaynes_items)
@crew = [mal, jayne]
render_odt 'app/template/showcase.odt'
end
end
3我尝试了包含日语的模板,但出现了错误。在gem(template.rb 22)中的以下点引发的错误
def进程上下文
tmpfiles=[]
Zip::ZipFile.open(@template)do | ZipFile|
%w(content.xml styles.xml)。每个do | xml|u文件|
content=zipfile.read(xml\u文件)
odteruby=odteruby.new(XmlReader.new(content))
out=odteruby.evaluate(上下文)
tmpfiles这有点老了,但我遇到了同样的情况。在我的例子中,这是一个在写入tempfile之前将其设置为二进制模式的问题:
tmpfiles << (file = Tempfile.new("serenity"))
file.binmode
file << out
file.close
tmpfiles感谢您的回复。但我很抱歉,这不适合我的案子。最后,我的问题通过使用theoo的分叉宝石解决了。在为接收为JSON的文件编写字节数组时,这让我松了一口气。正在写入文件,并收到类似错误。在fle上设置binmode是一个技巧。谢谢
def process context
tmpfiles = []
Zip::ZipFile.open(@template) do |zipfile|
%w(content.xml styles.xml).each do |xml_file|
content = zipfile.read(xml_file)
odteruby = OdtEruby.new(XmlReader.new(content))
out = odteruby.evaluate(context)
tmpfiles << (file = Tempfile.new("serenity"))
file << out #!!!! HERE !!!!
file.close
zipfile.replace(xml_file, file.path)
end
end
end
tmpfiles << (file = Tempfile.new("serenity"))
file.binmode
file << out
file.close