[英]Rails app not recognizing the fastercsv gem on the production server
[英]Rails 3 Server Startup problem with fastercsv
我有一個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
如果在循環或代碼中使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.