# File is nil

**URL:** <https://www.ruby-forum.com/t/file-is-nil/95598>\
**Category:** Rails\
**Created:** [June 7, 2006, 3:30am UTC](https://www.ruby-forum.com/t/file-is-nil/95598 "2006-06-07T03:30:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lisa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f08c70/32.png) [@lisa](https://www.ruby-forum.com/u/lisa)\
**Post date:** [June 7, 2006, 3:30am UTC](https://www.ruby-forum.com/t/file-is-nil/95598/1 "2006-06-07T03:30:02Z")

</div>

I don’t understand why my program does not get the file that I try to  
upload.  
It returns the following error when I try to upload a small file:

NoMethodError in LogfileController#create

You have a nil object when you didn’t expect it!  
The error occured while evaluating nil.original\_filename

#{RAILS\_ROOT}/app/models/logfile.rb:9:in `file=' #{RAILS_ROOT}/app/controllers/logfile_controller.rb:31:in `create’

Request  
Parameters: {“logfile”=\> {“fileName”=\>#StringIO:0x39c3fc0},  
“commit”=\>“Create”}

This is the code in view:

\<%= form\_tag({:action =\> ‘simple\_save’},  
{:method =\> “post”, :multipart =\> true}) %\>  
  
\<%= submit\_tag “Simple Save” %\>  
\<%= end\_form\_tag %\>

This is the code in the controller:

def create  
…  
@logfile.file= @params[‘logfile\_fileName’]  
…  
end

And the Model is nearly identical to the code in here:

[BigBold - Informasi Tentang Bisnis dan Marketing](http://www.bigbold.com/snippets/posts/show/1805)

Is it because I don’t have a file field in my database? The database  
keeps the name of the file and some information related to it. I want  
to save the file locally only, not to the database.

Please help! I’ve been stuck for DAYS.

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![lisa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f08c70/32.png) [@lisa](https://www.ruby-forum.com/u/lisa)\
**Post date:** [June 7, 2006, 4:29am UTC](https://www.ruby-forum.com/t/file-is-nil/95598/2 "2006-06-07T04:29:41Z")

</div>

Oh. My. Goodness.

What was missing was the the length of character input the html.  
So I went overboard with the input size.

My view now reads:

---

<div class="post-metadata">

**Author:** ![lisa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f08c70/32.png) [@lisa](https://www.ruby-forum.com/u/lisa)\
**Post date:** [June 7, 2006, 12:55pm UTC](https://www.ruby-forum.com/t/file-is-nil/95598/3 "2006-06-07T12:55:29Z")

</div>

Hi –

On Wed, 7 Jun 2006, Lisa wrote:

> #{RAILS\_ROOT}/app/controllers/logfile\_controller.rb:31:in `create’
> 
> Request  
> Parameters: {“logfile”=\> {“fileName”=\>#StringIO:0x39c3fc0},  
> “commit”=\>“Create”}  
> […]  
> @logfile.file= @params[‘logfile\_fileName’]

Those don’t match. Try:

@logfile.file = params[‘logfile’][‘fileName’]

David

–  
David A. Black (removed\_email\_address@domain.invalid)

- Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

> Ruby and Rails consultancy and training

- Author of “Ruby for Rails” from Manning Publications!

> [Ruby for Rails](http://www.manning.com/black)

---

<div class="post-metadata">

**Author:** ![lisa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f08c70/32.png) [@lisa](https://www.ruby-forum.com/u/lisa)\
**Post date:** [June 7, 2006, 12:55pm UTC](https://www.ruby-forum.com/t/file-is-nil/95598/4 "2006-06-07T12:55:50Z")

</div>

Here’s your problem:

2006/6/7, Lisa [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Request  
> Parameters: {“logfile”=\> {“fileName”=\>#StringIO:0x39c3fc0},  
> “commit”=\>“Create”}

> ```
> @logfile.file= params['logfile_fileName']
> 
> ```

You’re tring to access the ‘logfile\_fileName’ key of params, but  
params only has a ‘logfile’ and a ‘commit’ key. But, the param  
‘logfile’ is itself a hash, with a key ‘fileName’, so what you really  
want is:

@logfile.file= params[‘logfile’][‘fileName’]

When you have an underscore in the param name, Rails groups the params  
by prefix. Here, the prefix is ‘logfile’, so you get a hash saved  
under ‘logfile’ with all the params which start ‘logfile\_’, rather  
than accessing the params ‘logfile’ itself.

This is especially useful when, say, you want to create two model  
objects, and you want to initialise them from a hash. This way you can  
have fileds ‘alpha\_name’, ‘alpha\_address’ and ‘beta\_name’ and  
‘beta\_age’, which will give you two hashes, params[‘alpha’] and  
params[‘beta’]. Have a look in the documentation under form helpers to  
learn more about how this works.

hth,  
Douglas

---

<div class="post-metadata">

**Author:** ![lisa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f08c70/32.png) [@lisa](https://www.ruby-forum.com/u/lisa)\
**Post date:** [June 7, 2006, 8:24pm UTC](https://www.ruby-forum.com/t/file-is-nil/95598/5 "2006-06-07T20:24:31Z")

</div>

Thank you all so much!

Everyone in the Ruby community has been so helpful.

I have to admit this is scaffold withdrawal, but I honestly tried to  
read the documentation (I just didn’t know where to start). Much of it  
is still magic and mystics to me, but I hope to become better at it.

Thanks again.

Lisa

---

<div class="post-metadata">

**Author:** ![lisa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f08c70/32.png) [@lisa](https://www.ruby-forum.com/u/lisa)\
**Post date:** [June 7, 2006, 1:02pm UTC](https://www.ruby-forum.com/t/file-is-nil/95598/6 "2006-06-07T13:02:29Z")

</div>

It’s not `@params['logfile_fileName']` but  
`params[:logfile]["fileName"]`
