开发者

Rails 3 Server Startup problem with fastercsv

I have a rails 2.3.5 app getting upgraded to Rails 3. I did every thing I am required to do for upgrading and when I start the rails server using

rails server

it gives me this

Please switch to Ruby 1.9's standard CSV library.  It's FasterCSV plus
support for Ruby 1.9's m17n encoding engine.

I am using ruby-1.9.2-p0 and have fastercsv (1.5.3) gem installed. with the help of puts statements, i was able to track down where the error occurred. i found that the execution stops 开发者_高级运维at this line

Bundler.require(:default, Rails.env) if defined?(Bundler)

in application.rb file. I tried many things but none worked .. please help..


Remove fasterCSV from your Gemfile in the application. Bundler is trying to require FasterCSV because you have it specified in the Gemfile.


with 1.9 you no longer need/can use the fastercsv gem, since it's bundled in the std lib. Now you just need to do:

require 'csv'
CSV.open("temp.csv", "w") do |csv|
  csv << ["line1row1", "line1row2"]
  csv << ["line2row1", "line2row2"]
  # ...
end


This is the solution i found:

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


Look here too http://www.wherethebitsroam.com/blogs/jeffw/fastercsv-csv-ruby-18-19-and-rails-30


If used FasterCsv in loop or in the code just change it with Csv and works for me. Remove gem 'fastercsv' from gem file. Just write your code in controller, no need add other code in somewhere in config.

This is the example of wrong code.

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

and just corrected with changing word FasterCsv to Csv and it works. like below

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜