Unexpected 'nil' in rails upload application

Hi All,

Please help. I am sure there is something obvious I am overlooking in
the code but I cannot see it. Please note, as detailed below, I am on
Bluehost using Rails 2.2.2.

The code is example code from TutorialsPoint

On bluehost

Issue Summary:

  • ‘nil’ whereas it should be variables being passed from
    uploadfile.rhtml
  • Line 3 in [app/models] data_file.rb
    name = upload[‘datafile’].original_filename # Line 3
  • Line 7 in [app/controllers] upload_controller.rb
    post = DataFile.save(params[:upload]) # Line 7

Regards

Environment information

Hosting service: Bluehost

Rails Environment

Ruby version 1.8.6 (x86_64-linux)
RubyGems version 1.3.1
Rails version 2.2.2
Active Record version 2.2.2
Action Pack version 2.2.2
Active Resource version 2.2.2
Action Mailer version 2.2.2
Active Support version 2.2.2
Application root ~/rails/upload
Environment development
Database adapter sqlite3
Database schema version 0

Relevant source files

dir: [app/models] file: data_file.rb

class DataFile < ActiveRecord::Base
def self.save(upload)
name = upload[‘datafile’].original_filename # Line 3

directory = "public/data"
# create the file path
path = File.join(directory, name)
# create or over-write file 'path' with 'datafile' contents
File.open(path, "wb") { |f| f.write(upload['datafile'].read) }

end
end

dir: [app/controllers] file: upload_controller.rb

class UploadController < ApplicationController
def index
render :file => ‘upload/uploadfile.rhtml’
end

def uploadFile
post = DataFile.save(params[:upload]) # Line 7
render :text => “File has been uploaded successfully”
end

end

dir: [app/views/upload] file: uploadfile.rhtml

File Upload

<% form_tag :action => 'uploadFile', :multipart => true do %>

Select File :

<%= file_field ‘upload’, ‘datafile’ %>

<%= submit_tag “Upload” %>
<% end %>

run application

http://upload.datajazz.com/upload/index

Error Output

NoMethodError in UploadController#uploadFile

You have a nil object when you didn’t expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
RAILS_ROOT: ~/rails/upload

Application Trace | Framework Trace | Full Trace
app/models/data_file.rb:3:in save' app/controllers/upload_controller.rb:7:in uploadFile’

Request
Parameters:

{“multipart”=>“true”,
“commit”=>“Upload”,
“authenticity_token”=>“82b3eeae67198fb71bb4dea43b1790ac70bda0ae”}
Show session dump

Response

Headers:

{“cookie”=>[],
“Content-Type”=>“”,
“Cache-Control”=>“no-cache”}

Hi All,

As I suspected, it was a very simple mistake in the original code
which I had picked up from the web.
Issue: the destination path didn’t contain a valid directory.
I suspect the original code probably worked on a different platform
(probably Windows).

Here is the complete working solution, which I have rewritten to
eliminate some of the older rails code.

Regards
************ Working solution

dir: [app/models] file: data_file.rb

class DataFile < ActiveRecord::Base
def self.save(upload)
# Changed Default Destination: [RAILS_DIR/public/data]
name = “data/” + upload.original_filename
File.open(name, “wb”) { |f| f.write(upload.read) }
end
end

dir: [app/controllers] file: upload_controller.rb

class UploadController < ApplicationController
def index
render :file => ‘upload/uploadfile.rhtml’
end

def uploadFile
file_param = params[:upload][:datafile]
post = DataFile.save(file_param)
render :text => “File has been uploaded successfully”
end
end

dir: [app/views/upload] file: uploadfile.rhtml

File Upload

<% form_for :upload,:url=>{:action => 'uploadFile'},:html => { :multipart => true } do |f| %>

Select File : <%= f.file_field 'datafile' %>

<%= f.submit "Upload" %> <% end %>

run application

http://upload.datajazz.com/upload/index