Use case: Upload and process a csv file and record the upload session in the database.
Approach: Create a model to hold data about the upload session and a controller with a new method that collects the csv file and a create method that creates, populates and saves an upload object.
Problem: The model includes an initialize method (see Model code) that seems to have the necessary information to initialize the object (see debug output), but, when the controller attempts to use the model's new method, the attempt does not result in a useful object (see controller code and debug output).
Question: What do I need to change to create the call_history_upload object?
Ruby 1.9.3 on Rails 3.2.13 on Windows 8.1
Model Code:
class CallHistoryUpload < ActiveRecord::Base
attr_accessible :file_name, :user_id, :record_count, :international_call_count, :unknown_client_count
has_many :call_histories, dependent: :destroy
require 'csv'
def initialize( file_name, user_id )
logger.debug( "CallHistoryUpload.initialize start")
logger.debug( " call_history_file = " + file_name )
logger.debug( " user_id = " + user_id )
@file_name = file_name
@user_id = user_id
@record_count = 0
@international_call_count = 0
@unknown_client_count = 0
logger.debug( "CallHistoryUpload.initialize end")
end
end
Controller code:
class CallHistoryUploadsController < ApplicationController
def get_client_id
-1
end
# GET /call_history_uploads/new
# GET /call_history_uploads/new.json
def new
@call_history_upload = CallHistoryUpload.new( "Unknown", session[:user_id] ) # Needed to suppoprt json
respond_to do |format|
format.html # new.html.erb
format.json { render json: @call_history_upload }
end
end
# POST /call_history_uploads
# POST /call_history_uploads.json
def create
logger.debug( "CallHistoryUploadsController start")
@call_history_upload = CallHistoryUpload.new( params[:call_history_file].original_filename, session[:user_id] )
logger.debug( "CallHistoryUploadsController after instantiation")
<<This is line 24>>logger.debug( "call_history_upload.file_name = " + @call_history_upload.file_name )
respond_to do |format|
if @call_history_upload.save
format.html { redirect_to @call_history_upload, notice: 'Call history upload was successful.' }
format.json { render json: @call_history_upload, status: :created, location: @call_history_upload }
else
format.html { render action: "new" }
format.json { render json: @call_history_upload.errors, status: :unprocessable_entity }
end
end
end
end
log output:
Started POST "/call_history_uploads" for 127.0.0.1 at 2014-11-30 08:40:27 -0500
Processing by CallHistoryUploadsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"iDugtKa8b/U9q71Gk86sJMK6hnX1gvgQt496PX2q4Oo=", "call_history_file"=>#<ActionDispatch::Http::UploadedFile:0x4717788 @original_filename="Test_kiyvivzokpysxilfoftavyuftot.csv", @content_type="application/vnd.ms-excel", @headers="Content-Disposition: form-data; name=\"call_history_file\"; filename=\"Test_kiyvivzokpysxilfoftavyuftot.csv\"\r\nContent-Type: application/vnd.ms-excel\r\n", @tempfile=#<File:C:/Users/Gene/AppData/Local/Temp/RackMultipart20141130-5144-yn8i9>>, "commit"=>"Submit"}
CallHistoryUploadsController start
CallHistoryUpload.initialize start
call_history_file = Test_kiyvivzokpysxilfoftavyuftot.csv
user_id = KinteraAdmin
CallHistoryUpload.initialize end
CallHistoryUploadsController after instantiation
Completed 500 Internal Server Error in 0ms
NoMethodError (undefined method `has_key?' for nil:NilClass):
app/controllers/call_history_uploads_controller.rb:24:in `create'
Rendered C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.0ms)
Rendered C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.0ms)
Rendered C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (0.0ms)