通过导出CSV文件,变音符号不起作用

时间:2014-01-06 15:02:44

标签: ruby-on-rails ruby csv encoding

我在CSV文件中使用变音符号(如“ä”,“ü”...)导出数据时出现问题。我试过不同的解决方案,但没有任何作用这是我的代码:

控制器:

  def bom_export
    @configurations = []
    @bom = []

    params[:configurations].each do |configuration_id|
      @configurations << Configuration::Configuration.find(configuration_id.to_i)
    end

    @configurations.each do |conf|
      conf.bill_of_materials.each do |material|
        @bom << material
      end
    end

    respond_to do |format|
      format.csv {
        csv_string = CSV.generate do |csv|
          csv << ["Id", "Titel", "Beschreibung", "Breite", "Höhe", "Länge"]
          @bom.each do |bom|
            csv << [bom.id, bom.title, bom.description, bom.width, bom.height, bom.length]
          end
        end

        c = Iconv.new('ISO-8859-15','UTF-8')
        send_data(c.iconv(csv_string),
                  :type => 'text/csv; charset=iso8859-1; header=present',
                  :filename => "test.csv")

      }
    end
  end

服务器日志:

Started GET "/admin/customer_orders/61/configuration_configurations" for 127.0.0.1 at 2014-01-06 16:27:31 +0100

SyntaxError (/Users/shop-it1/workspace/shop/app/controllers/configuration/configurations_controller.rb:106: invalid multibyte char (US-ASCII)
/Users/shop-it1/workspace/shop/app/controllers/configuration/configurations_controller.rb:106: invalid multibyte char (US-ASCII)
/Users/shop-it1/workspace/shop/app/controllers/configuration/configurations_controller.rb:106: syntax error, unexpected $end, expecting ']'
… "Beschreibung", "Breite", "Höhe", "Länge"]
…                               ^):


Rendered /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.0.10/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (42.2ms)
Error during failsafe response: ActionView::Template::Error
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.0.10/lib/active_support/core_ext/string/output_safety.rb:23:in `gsub'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.0.10/lib/active_support/core_ext/string/output_safety.rb:23:in `html_escape'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.0.10/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb:7:in `___sers_shop_it___rvm_gems_ruby_______p__gems_actionpack________lib_action_dispatch_middleware_templates_rescues_diagnostics_erb___3189230765086009151_70220166701020_1793258824290671195'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.0.10/lib/action_view/template.rb:135:in `block in render'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.0.10/lib/active_support/notifications.rb:54:in `instrument'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.0.10/lib/action_view/template.rb:127:in `render'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.0.10/lib/action_view/render/rendering.rb:59:in `block in _render_template'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.0.10/lib/active_support/notifications.rb:52:in `block in instrument'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.0.10/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.0.10/lib/active_support/notifications.rb:52:in `instrument'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.0.10/lib/action_view/render/rendering.rb:56:in `_render_template'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.0.10/lib/action_view/render/rendering.rb:26:in `render'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.0.10/lib/action_dispatch/middleware/show_exceptions.rb:88:in `rescue_action_locally'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.0.10/lib/action_dispatch/middleware/show_exceptions.rb:68:in `render_exception'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.0.10/lib/action_dispatch/middleware/show_exceptions.rb:59:in `call'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.0.10/lib/rails/rack/logger.rb:13:in `call'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/rack-1.2.4/lib/rack/runtime.rb:17:in `call'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.0.10/lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/rack-1.2.4/lib/rack/lock.rb:11:in `block in call'
  <internal:prelude>:10:in `synchronize'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/rack-1.2.4/lib/rack/lock.rb:11:in `call'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.0.10/lib/action_dispatch/middleware/static.rb:30:in `call'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.0.10/lib/rails/application.rb:168:in `call'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.0.10/lib/rails/application.rb:77:in `method_missing'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.0.10/lib/rails/rack/log_tailer.rb:14:in `call'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/rack-1.2.4/lib/rack/content_length.rb:13:in `call'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/rack-1.2.4/lib/rack/chunked.rb:15:in `call'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/thin-1.3.1/lib/thin/connection.rb:80:in `block in pre_process'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/thin-1.3.1/lib/thin/connection.rb:78:in `catch'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/thin-1.3.1/lib/thin/connection.rb:78:in `pre_process'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/thin-1.3.1/lib/thin/connection.rb:53:in `process'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/thin-1.3.1/lib/thin/connection.rb:38:in `receive_data'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run_machine'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/thin-1.3.1/lib/thin/backends/base.rb:61:in `start'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/thin-1.3.1/lib/thin/server.rb:159:in `start'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/rack-1.2.4/lib/rack/handler/thin.rb:14:in `run'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/rack-1.2.4/lib/rack/server.rb:217:in `start'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.0.10/lib/rails/commands/server.rb:65:in `start'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.0.10/lib/rails/commands.rb:30:in `block in <top (required)>'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.0.10/lib/rails/commands.rb:27:in `tap'
  /Users/shop-it1/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.0.10/lib/rails/commands.rb:27:in `<top (required)>'
  /Users/shop-it1/workspace/shop/script/rails:6:in `require'
  /Users/shop-it1/workspace/shop/script/rails:6:in `<top (required)>'
  -e:1:in `load'
  -e:1:in `<main>'

尝试保存CSV文件后,服​​务器崩溃(500)。没有变音符号一切正常。

3 个答案:

答案 0 :(得分:0)

您的问题是源文件中的umlats,您从未将其声明为除US-ASCII之外的任何内容。将此评论添加到源文件的顶部:

#encoding: UTF-8

注意:不要使用Iconv,不推荐使用它,并且已经在Ruby 1.9中添加了编码支持。使用Ruby 1.9的内置编码方法。例如:

send_data(csv_string.encode("ISO-8859-1"),
              :type => 'text/csv; charset=iso8859-1; header=present',
              :filename => "test.csv")

答案 1 :(得分:0)

我找到了解决方案!

问题不在于导出,而在于脚本本身的编码。

在我的Controller-Script开头添加以下行解决了这个问题:

#!/bin/env ruby
# encoding: utf-8

答案 2 :(得分:0)

对我而言,这就是诀窍

send_data(csv_string.encode("UTF-8"),
          :type => 'text/csv; charset=UTF-8; header=present',
          :filename => "test.csv")

# encoding: utf-8

没有帮助