I'm new to Rails, and I figured out how to export the results from my database, but I'm having trouble creating new records from a CSV file. With the code below, I want to be able to import a CSV file and populate the last two columns with session data from the user. For now, I just inserted a static number to try to get this to work. I am currently receiving "Unable to convert ActionDispatch :: Http :: UploadedFile to String" as an error message
DOCUMENT CSV
name, tasks, expected_results, require_id Name of test 1, Open file, Open, t Name of test 2, Read file, Read, t Name of test 3, Close file, Close, f
CONTROLLER
def csv_import
file = params[:file]
FasterCSV.foreach(file,{:headers => true, :row_sep => :auto}) 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
end
VIEW
<%=form_tag '/script_admin/csv_import', :multipart => true do%>
<%= file_field_tag "file" %><br/>
<%= submit_tag("Import") %>
<% end %>
DB MIGRATION
class CreateScripts < ActiveRecord::Migration
def self.up
create_table :scripts do |t|
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, :default => "t"
t.timestamps
end
end
def self.down
drop_table :scripts
end end
~