2014-01-06 26 views
-1

我有一個問題,出口與元音變音數據(如「ä」,「ü」...)在CSV文件。我嘗試了不同的解決方案,但沒有任何效果這裏是我的代碼:變音不起作用,通過導出一個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)之後。沒有變音器一切正常。

+0

也許那裏是另一種沒有Iconv的解決方案,因爲Iconv已被棄用(?) – levitas111

+0

請發佈您在'log/development.log'中獲得的錯誤消息。 – phoet

+0

UTF-8應該與德文字符集一起工作得很好。爲什麼你甚至會轉換它? – phoet

回答

0

你的問題是源文件中的錯誤,你永遠不會聲明爲除了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") 
+0

是他的問題的正確答案? (服務器崩潰) – phoet

+0

感謝您的建議@Linuxios,但問題仍然是一樣的:(任何其他想法爲什麼變音符號「ü」,「ä」...不工作? – levitas111

+0

@ user1542555,phoet:請參閱編輯。 – Linuxios

0

我找到了解決方案!

問題不在於導出,而在於腳本本身的編碼。

在我的控制腳本的開頭添加以下行解決了這個問題:

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

第一行是一個shebang,只對可執行腳本是必需的,所以它不會被添加到控制器文件中 – phoet

+0

謝謝@phoet!你當然是對的! – levitas111

0

對我來說這並獲得成功

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

# encoding: utf-8 

沒」 t幫助