未定义的方法" map" for String - 将字符串压入CSV时出错

时间:2015-03-23 20:40:34

标签: ruby-on-rails ruby csv

我在Rails服务器中为生成的报告文件提供CSV格式。

完整性测试:     2.2.0-preview1:001> a = []      => []     2.2.0-preview1:002> a<< " 9"      => [" 9"]

代码:

    def to_csv
        CSV.generate do |csv|
          csv << column_header_titles
          products.each do |p|
            write_product_array(p).each do |text|
              csv << text
            end
          end
        end
      end

def write_product_array(product)
    [container_name(product), 
    product.name, 
    product.previous_quantity.to_s,
    ... etc, all strings.

错误:

/vendor/ruby-2.2.0/lib/ruby/2.2.0/csv.rb:1673 in "<<"
/app/reports/schedule_machine_reconciliation_report.rb:13 in "block (3 levels) in to_csv"
/app/reports/schedule_machine_reconciliation_report.rb:12 in "each"
/app/reports/schedule_machine_reconciliation_report.rb:12 in "block (2 levels) in to_csv"
/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/relation/delegation.rb:46 in "each"
/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/relation/delegation.rb:46 in "each"
/app/reports/schedule_machine_reconciliation_report.rb:11 in "block in to_csv"
/vendor/ruby-2.2.0/lib/ruby/2.2.0/csv.rb:1157 in "generate"
/app/reports/schedule_machine_reconciliation_report.rb:9 in "to_csv"
/app/reports/schedule_machine_reconciliation_report.rb:30 in "to_csv"
/app/controllers/api/v1/schedule_machine_renconciliation_reports_controller.rb:34 in "block (2 levels) in create"
/vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.0/lib/action_controller/metal/mime_responds.rb:216 in "call"
/vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.0/lib/action_controller/metal/mime_responds.rb:216 in "respond_to"
/app/controllers/api/v1/schedule_machine_renconciliation_reports_controller.rb:32 in "create"
/vendor/bundle/ruby/2.2.0/gems/apipie-rails-0.2.6/lib/apipie/dsl_definition.rb:225 in "call"
/vendor/bundle/ruby/2.2.0/gems/apipie-rails-0.2.6/lib/apipie/dsl_definition.rb:225 in "block in _apipie_define_validators"
/vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.0/lib/action_controller/metal/implicit_render.rb:4 in "send_action"
/vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.0/lib/abstract_controller/base.rb:198 in "process_action"
/vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.0/lib/action_controller/metal/rendering.rb:10 in "process_action"
/vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.0/lib/abstract_controller/callbacks.rb:20 in "block in process_action"
/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:117 in "call"
/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:117 in "call"

1 个答案:

答案 0 :(得分:2)

试试这个,我记得你需要一个数组来添加到csv

def to_csv
   CSV.generate do |csv|
     csv << column_header_titles
     products.each do |p|
         csv << write_product_array(p)
       end
     end
   end
end