I have a written a simple RoR program that accepts a plaintext upload from a user and then emails that file capitalized to an email address aquired through a form. I am trying to utilize ActiveRecord to create a list of past upload , but I receive a NoMethodError whenever I try to save my model. The error follows:
<code>NoMethodError in UploadController#upload
You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.delete
RAILS_ROOT: C:/RailsApps/SpecEd1 Application Trace:
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:124:in `rollback_active_record_state!' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:110:in `save!' app/controllers/upload_controller.rb:18:in `upload'
Request
Parameters:
{"Upload"=>"Upload, Capitalize, and Email", "emailaddress"=>"Orbipedis@gmail.com", "datafile"=>#<ActionController::UploadedStringIO:0x3442a34>}
Response
Headers:
{"cookie"=>, "Cache-Control"=>"no-cache"}</code>
And here is my Model: require 'net/smtp'
class DataFile < ActiveRecord::Base def initialize(incoming_file) @file_name = incoming_file.original_filename @content_type = incoming_file.content_type @file = incoming_file end def uploaded_file=(incoming_file) @file_name = incoming_file.original_filename @content_type = incoming_file.content_type @file = incoming_file end def file_name=(new_file_name) write_attribute("file_name", sanitize_filename(new_file_name)) end def save_file File.open("#{RAILS_ROOT}/public/uploaded/#{@file_name}", "wb") do |f| f.write(@file.read) end end def email(address, time) filedata = "" File.open("#{RAILS_ROOT}/public/uploaded/#{@file_name}", "r") do |f| if @content_type === 'text/plain' lines = f.readlines Net::SMTP.start('smtp.psu.edu', 25, 'arl.psu.edu', 'zkg5001', '4b4eebhjkasdfyu', :plain) do |smtp| smtp.send_message(generate_email(lines, time), 'zkg5001@psu.edu', address) end else Net::SMTP.start('smtp.psu.edu', 25, 'arl.psu.edu', 'zkg5001', '4b4eebhjkasdfyu', :plain) do |smtp| smtp.send_message("file was not plaintext", 'zkg5001@psu.edu', address) end end end end def to_s "File-- name:#{@file_name} type:#{@content_type}" end private def sanitize_filename(file_name) just_filename = File.basename(file_name) just_filename.gsub(/[^\w\.\-]/,'_') end def generate_email(lines, time) email = "Time of fileprocess is: File processed at: #{time.hour}:#{time.min}:#{time.sec} #{time.mon}/#{time.mday}/#{time.year}\n\n" email << lines.join('\n').upcase File.open("C:\\myLog.txt", "w") do |f| f.puts(email) end return email end end
And its migration: class DataFiles < ActiveRecord::Migration def self.up create_table :data_files do |t| t.column :file_name, :string t.column :content_type, :string t.column :file, :text end end
def self.down drop_table :data_files end end
and the controller: require 'date'
class UploadController < ApplicationController protect_from_forgery :only => [:create, :update, :destroy] def index render :file => 'app\views\upload\index.html.erb' end def upload @myData = DataFile.new(params['datafile']) File.open("C:/myLog.txt", "w") do |f| f.puts @myData end #@myData.uploaded_file = params['datafile'] @myData.save_file @myData.save! timeOfUpload = DateTime.now if(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.match(params['emailaddress'])) @myData.email(params['emailaddress'], timeOfUpload) redirect_to :action => 'index' else redirect_to :action => 'invalid_email' end end def invalid_email render :file => 'app/views/upload/invalid_email.html.erb' end def history render :file =>'app/views/upload/history.html.erb' end end I don't know why the exception is being thrown or how to fix it. Any assistance would be much appreciated