首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >‘编码:使用红宝石rails中使用file.open的一些红宝石时,ASCI-8bit到UTF-8’的未定义转换错误“\xE9 9”

‘编码:使用红宝石rails中使用file.open的一些红宝石时,ASCI-8bit到UTF-8’的未定义转换错误“\xE9 9”
EN

Stack Overflow用户
提问于 2012-10-26 03:04:18
回答 1查看 3K关注 0票数 4

我想要什么?

我想从Rails 3.2中的模板文件生成.docx文件或.odt文件

我想在里面用日语。

在ubuntu服务器12.04和ruby 1.9.3p194和rails 3.2.8中

发生什么事了?

我试过宝石'docx-templater‘和’宁静‘

红宝石-

https://github.com/jawspeak/ruby-docx-templater

1样品工作良好

2尝试在我的rails应用程序中做同样的事情。

在控制器中作为样本

代码语言:javascript
复制
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)

代码语言:javascript
复制
File.open(file_name, 'w') { |f| f.write(buffer) }

安详

https://github.com/kremso/serenity

1个样本运行良好

2在我的rails应用程序中做同样的事情,并且运行良好。

像这样

代码语言:javascript
复制
#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)

代码语言:javascript
复制
    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

我做了什么?

  • 我发现‘宁静’有rspec测试的希腊语(UTF-8)在宝石,我尝试了日语同样的方式。考试通过了!所以我认为问题不在于rails设置中的宝石。
  • 在我的控制器或lib文件中添加神奇的注释“# file : utf-8”
  • 在config/application.rb中确认'config.encoding =“utf-8”
  • 在“初始化!”上面添加以下内容到config/Environroment.rb Encoding.default_external = Encoding::UTF_8 Encoding.default_internal = Encoding::UTF_8

  • 我在rails应用程序中不使用任何数据库。

但这与我的案子无关..。知道吗?我可能不知道基本的事情..。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-12 17:01:22

这有点旧,但我只是碰到了同样的情况。在我的例子中,问题是在编写这个文件之前,将它设置为二进制模式:

代码语言:javascript
复制
  tmpfiles << (file = Tempfile.new("serenity"))
  file.binmode
  file << out   
  file.close

希望这能有所帮助

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13080178

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档