 |
Forums |
Admin Discussion Forums: help Start New Thread
By: James Gray
RE: Help setting up FasterCSV to import a file [ reply ] 2009-05-13 19:57
|
That's a good question.
I can build a CSV file that is a big resource drain on FasterCSV's parser yes. Uploading that would be kind of like a DoS attack on your server.
However, there are ways you could combat this.
One option would be limit the size of what users can upload to something reasonable. You can do this with Rack middleware if you are using the latest version of Rails.
Another option is to wrap the parse in a Timeout.timeout { } call, so you cancel it if the parse gets stuck in a nasty data loop.
I hope that gives you some fresh ideas.
|
By: James Gray
RE: Help setting up FasterCSV to import a file [ reply ] 2009-05-13 14:53
|
Try switching this line:
FasterCSV.parse(file, :headers => true) do |row|
To what I suggested in my last message:
FCSV.new(file, :headers => true).each do |row|
I bet that fixes you up.
The reason this happened is that Rails made a small change on how they pass up the data. You are probably running on a newer version that the other code was written for.
Hope that helps.
|
By: Demetrius Olsen
RE: Help setting up FasterCSV to import a file [ reply ] 2009-05-13 04:46
|
Sorry for not being very clear. I'm a newbie :-)
I want to allow teachers to import students via a CSV file. In my controller, I have:
require 'fastercsv'
class ImportStudentController < ApplicationController
def index
end
def csv_import
file = params[:csv_import][:file]
logcount=0
Student.transaction do
FasterCSV.parse(file, :headers => true) do |row|
current_user.students.create!(row.to_hash)
logcount += 1
end
end
flash[:notice] = "Successfully added #{logcount} Student(s)."
redirect_to :action => :index
rescue => exception
# If an exception is thrown, the transaction rolls back and we end up in this rescue block
error = ERB::Util.h(exception.to_s) # get the error and HTML escape it
flash[:error] = "Error adding logs. (#{error}). Please try again."
redirect_to :action => :index
end
end
In my view, I have:
<% if flash[:notice] -%>
<%= flash[:notice] %>
<% end -%>
<% if flash[:error] -%>
<%= flash[:error] %>
<% end -%>
<% form_for :csv_import, :url=>{ :controller=>"import_student", :action => 'csv_import'},
:html => { :multipart => true } do |f| %>
<p><%= f.label :file, 'Import Students file' %><br/>
<%= f.file_field :file -%></p>
<p><%= submit_tag "Submit" %></p>
<% end %>
These files work in another app that I found online, but not in mine. My original question should have been, what am I missing? Why would this code work in one app but not in mine? Thanks again.
|
By: James Gray
RE: Help setting up FasterCSV to import a file [ reply ] 2009-05-13 03:22
|
I'm not super sure I understood the question, but it sounds like you want FasterCSV to read from the incoming IO object in your Rails parameters. If so, you can probably use something like:
FCSV.new(params[:file]).each do |row|
# use row here...
end
I hope that helps.
|
By: Demetrius Olsen
Help setting up FasterCSV to import a file [ reply ] 2009-05-13 00:47
|
I am doing my best to configure/use FasterCSV to import a CSV file but keep getting an error message:
"Error adding logs. (You have a nil object when you didn't expect it! The error occurred while evaluating nil.students). Please try again."
When I look at the console window, I notice that within the parameters it says {"file"=>#<File:C:/Users/DEMETR~1/AppData/Local/Temp/RackMultipart20090512-4252-1mr70he-0>}. This is not the correct directory of the file I wanted to upload.
I downloaded an app that uses FasterCSV (and am using the code from its controller and views in my app). With this app, I am able to successfully import a CSV file. From the console window, I noticed the file parameters read {"file"=>#<ActionController::UploadStringIO:0x72f8314>}
I'm wondering if there is a "specification file" that I need to configure because at one point I was prompted (from the console window) to run "rake gems:refresh_specs."
Thank you in advance.
|
|
 |