# Unexpected 'nil' in rails upload application

**URL:** <https://www.ruby-forum.com/t/unexpected-nil-in-rails-upload-application/172371>\
**Category:** Rails\
**Created:** [July 20, 2009, 2:40pm UTC](https://www.ruby-forum.com/t/unexpected-nil-in-rails-upload-application/172371 "2009-07-20T14:40:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![GuruOne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c67d28/32.png) [@GuruOne](https://www.ruby-forum.com/u/GuruOne)\
**Post date:** [July 20, 2009, 2:40pm UTC](https://www.ruby-forum.com/t/unexpected-nil-in-rails-upload-application/172371/1 "2009-07-20T14:40:26Z")

</div>

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

- URL:  
[Ruby on Rails - File Uploading | Tutorialspoint](http://www.tutorialspoint.com/ruby-on-rails/rails-file-uploading.htm)  
and modified to run on Linux bluehost.

On bluehost

- [http://upload.datajazz.com/](http://upload.datajazz.com/)  
gives standard ‘Welcome Page’
- [http://upload.datajazz.com/upload/index](http://upload.datajazz.com/upload/index)  
presents html page for file upload  
upload file selection works  
failure occurs on ‘Upload’ button click

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](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”}

---

<div class="post-metadata">

**Author:** ![GuruOne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c67d28/32.png) [@GuruOne](https://www.ruby-forum.com/u/GuruOne)\
**Post date:** [July 31, 2009, 3:26pm UTC](https://www.ruby-forum.com/t/unexpected-nil-in-rails-upload-application/172371/2 "2009-07-31T15:26:15Z")

</div>

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](http://upload.datajazz.com/upload/index)
