Forums | Admin

Discussion Forums: help

Start New Thread Start New Thread

 

By: Caleb Johnson
RE: File Upload Help [ reply ]  
2011-02-16 04:15
Brilliant..... Thank you so much for helping me with this issue.

By: James Gray
RE: File Upload Help [ reply ]  
2011-02-16 04:04
Yikes. We have to have at least gets() to use FCSV.

I looked in the docs, let's try this:

FCSV.new(params[:file].tempfile, :headers => true).each do |row|

Does that help?

By: Caleb Johnson
RE: File Upload Help [ reply ]  
2011-02-15 23:55
When I add :row_sep => "\n" I receive the following message now.
"Error adding logs. (some private method `gets' called for #<ActionDispatch::Http::UploadedFile:0x105695ae0>). Please try again."

def csv_import
FCSV.new(params[:file], :headers => true, :row_sep => "\n").each do |row|
Script.create(:name => row[0],
:task => row[1],
:expected_results => row[2],
:require_id => row[3],
:department_id => 1,
:category_id => 1)
end
flash[:notice] = "Successfully added Script(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. (some #{error}). Please try again."
redirect_to :action => :index
end

By: James Gray
RE: File Upload Help [ reply ]  
2011-02-15 23:37
pos() is only used to auto-guess the line endings. It looks like the Rails wrapper doesn't support it.

You should be able to set the endings manually to get around this. Just add a :row_sep => "\n" (or "\r\n") to the end of your call to FCSV.new().

Hope that helps.

By: Caleb Johnson
File Upload Help [ reply ]  
2011-02-15 22:58
I am currently having issues with configuring FasterCSV to upload a CSV file and having the information in the CSV file insert into my DB. Currently using SQLite for Development and MySQL for Production. I get the following error whenever I attempt to upload a csv file.

"Error adding logs. (some undefined method `pos' for #<ActionDispatch::Http::UploadedFile:0x1059a2b48>). Please try again."

Here are my files....

######CONTROLLER

def csv_import
FCSV.new(params[:file]).each do |row|
Script.create(:name => row[0],
:task => row[1],
:expected_results => row[2],
:require_id => row[3],
:department_id => 1,
:category_id => 1)
end
flash[:notice] = "Successfully added Script(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. (some #{error}). Please try again."
redirect_to :action => :index
end

######VIEW

<%= form_tag '/script_admin/csv_import', :multipart => true do %>
<%= file_field_tag "file" %><br/>
<%= submit_tag("Import") %>
<% end %>

######DB STRUCTURE

t.integer :department_id, :null => false
t.integer :category_id, :null => false
t.string :name, :null => false
t.string :task, :null => false
t.string :expected_results, :null => false
t.boolean :require_id, :null => false
t.timestamps

Any help would be appreciated.