使用fastercsv的Rails 3服务器启动问题
我有一个rails 2.3.5应用程序升级到Rails 3.我做了升级所需要做的每件事以及当我启动rails服务器时
rails server
它给了我这个
Please switch to Ruby 1.9's standard CSV library. It's FasterCSV plus support for Ruby 1.9's m17n encoding engine.
我使用ruby-1.9.2-p0
并安装了fastercsv (1.5.3) gem
。 在puts语句的帮助下,我能够找到错误发生的位置。 我发现执行在此行停止
Bundler.require(:default, Rails.env) if defined?(Bundler)
在application.rb文件中。 我尝试了很多东西,但都没有用..请帮忙..
从应用程序中的Gemfile中删除fastCSV。 Bundler试图要求FasterCSV,因为你在Gemfile中指定了它。
使用1.9你不再需要/可以使用fastercsv gem,因为它捆绑在std lib中。 现在你只需要做:
require 'csv' CSV.open("temp.csv", "w") do |csv| csv << ["line1row1", "line1row2"] csv << ["line2row1", "line2row2"] # ... end
这是我发现的解决方案:
require 'fastercsv' require 'csv' class ImportFileToAssetsWithFasterCsv < ActiveRecord::Migration def self.up if CSV.const_defined? :Reader csv = FasterCSV else csv = CSV end file = 'db/staticfiles/DB-good-rightnames-forimport.csv' csv.foreach(file) do |row| Asset.create!(:country => row[0], :city => row[1], :latlong => row[2], :XX => row[3], :DEC => row[4], :point_name => row[5], :system_type => row[6], :system_brand => row[7], :function => row[8], :operator_name => row[9], :operator_brand => row[10], :parent_company => row[11], :app => "WWW", :language => "en", :source => "XXX", :Z_name => "International", :pref_format => "") end end def self.down IspcAsset.destroy_all() end end
请看这里http://www.wherethebitsroam.com/blogs/jeffw/fastercsv-csv-ruby-18-19-and-rails-30
如果在循环或代码中使用FasterCsv只需用Csv更改它并适合我。 从gem文件中删除gem’fastcsv’。 只需在控制器中编写代码,无需在config中的某处添加其他代码。
这是错误代码的示例。
class HomeController < ApplicationController require 'fastercsv' def download_csv @invitation = Invitation.find(params[:id]) @activities = Version.where("created_at >= ?", @invitation.created_at) if params[:export] csv_string = FasterCSV.generate do |csv| # header row csv << ["Date", "Event", "Details"] @activities.each do |act| csv << [act.created_at.strftime("%d-%m-%Y"), act.event, act.item_id] end end timestamp = Time.now.strftime('%Y-%m-%d_%H:%M:%S') send_data csv_string, :type => 'text/csv; charset=iso-8859-1; header=present', :disposition => "attachment; filename=goal_history_#{timestamp}.csv" end end
并且只是通过将FasterCsv改为Csv来纠正它并且它有效。 如下
class HomeController < ApplicationController require 'csv' def download_csv @invitation = Invitation.find(params[:id]) @activities = Version.where("created_at >= ?", @invitation.created_at) if params[:export] csv_string = CSV.generate do |csv| # header row csv << ["Date", "Event", "Details"] @activities.each do |act| csv << [act.created_at.strftime("%d-%m-%Y"), act.event, act.item_id] end end timestamp = Time.now.strftime('%Y-%m-%d_%H:%M:%S') send_data csv_string, :type => 'text/csv; charset=iso-8859-1; header=present', :disposition => "attachment; filename=goal_history_#{timestamp}.csv" end end