2013-08-20 99 views
0

我目前使用此代碼:Rails的覆蓋CSV

CSV.open "application.csv", "a+" do |csv| 

    csv << [ "#{params[:first_name]}", "#{params[:last_name]}","#{params[:company]}","#{params[:email]}", "#{params[:phone]}", "#{params[:business]}", "#{params[:services]}", "#{params[:employees]}", "#{params[:turnover]}" ] 

end 

每個時間寫了一個額外的行到CSV,怎麼可以把0​​將覆蓋每次整個文件代替,因此它總是隻有一行?

回答

0

您應該使用'w'模式。順便說一句,你爲什麼寫"#{params[:first_name]}"哪裏params[:first_name]就夠了?該代碼應該看起來如下:

CSV.open 'application.csv', 'w' do |csv| 
    csv << [params[:first_name], params[:last_name], params[:company], params[:email], params[:phone], params[:business], params[:services], params[:employees], params[:turnover], params[:c4l_services]] 
end