# Testing for Form Submission?

**URL:** <https://www.ruby-forum.com/t/testing-for-form-submission/133691>\
**Category:** Rails\
**Created:** [April 2, 2008, 5:24pm UTC](https://www.ruby-forum.com/t/testing-for-form-submission/133691 "2008-04-02T17:24:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [April 2, 2008, 5:24pm UTC](https://www.ruby-forum.com/t/testing-for-form-submission/133691/1 "2008-04-02T17:24:56Z")

</div>

Hi-

I’m trying to create a file upload script using RoR but I am getting  
“You have a nil object when you didn’t expect it!” and I think this is  
caused by the fact that the no form has been submitted. Is that  
right?

Here’s the form:

\<% form\_for :dump, :url =\> { :controller =\> “mycontroller”, :action=\>  
“csv\_reader” }, :html =\> { :multipart =\> true } do |f| -%\>

| **_Select a CSV File:_** | \<%= file\_field\_tag 'file' %\> |
| \<%= submit\_tag "Submit" %\> |

\<% end %\>

Thanks!

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [April 2, 2008, 5:26pm UTC](https://www.ruby-forum.com/t/testing-for-form-submission/133691/2 "2008-04-02T17:26:06Z")

</div>

Not sure why that posted twice…

Anyhow, here’s how I call it in the controller:  
@file = CSV::Reader.parse(params[:dump][:file])

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [April 2, 2008, 5:43pm UTC](https://www.ruby-forum.com/t/testing-for-form-submission/133691/3 "2008-04-02T17:43:06Z")

</div>

On 2 Apr 2008, at 16:23, pete wrote:

> Hi-
> 
> I’m trying to create a file upload script using RoR but I am getting  
> “You have a nil object when you didn’t expect it!” and I think this is  
> caused by the fact that the no form has been submitted. Is that  
> right?

Maybe, maybe not. The error message should tell you what line the  
error occurred on, then you need to go and look at that line of code  
and work out what’s wrong. Also look at the params hash being submitted.

Fred

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [April 2, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/testing-for-form-submission/133691/4 "2008-04-02T18:00:17Z")

</div>

It’s definitely an empty params hash. It fails on the line I posted  
below:  
@file = CSV::Reader.parse(params[:dump][:file])

How can I figure out what exactly is being passed?

On Apr 2, 3:42 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [April 3, 2008, 10:35am UTC](https://www.ruby-forum.com/t/testing-for-form-submission/133691/5 "2008-04-03T10:35:21Z")

</div>

It’s because you have the file\_field\_tag ‘file’ line

if you want it to be inside dump, you need to do this:

f.file\_field\_tag ‘file’

as it stands, you will find the file by using params[:file] only.

Julian.

Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) NEW VIDEO  
(#2) OUT NOW!  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [April 3, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/testing-for-form-submission/133691/6 "2008-04-03T18:00:31Z")

</div>

Ok, this isn’t working at all. I have tried just about everything and  
it seems like it should be so simple.

I am constantly getting:  
NoMethodError in MyController#csv\_reader

You have a nil object when you didn’t expect it!

The file shouldn’t be there until the submit button is selected so why  
is it even looking for it?

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [April 3, 2008, 10:09pm UTC](https://www.ruby-forum.com/t/testing-for-form-submission/133691/7 "2008-04-03T22:09:02Z")

</div>

In case anyone has this same issue…

You can use this to ignore the file, before submission (controller):  
unless params[:file].nil?

And this is the correct code for the view:  
\<% form\_tag({:action=\> “parse\_csv”}, {:multipart =\> true}) do %\>

| **_Select a CSV File:_** | \<%= file\_field\_tag 'file' %\> |
| \<%= submit\_tag "Submit" %\> |

\<% end %\>

Note that the file is referenced as: params[:file]

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [April 4, 2008, 12:28am UTC](https://www.ruby-forum.com/t/testing-for-form-submission/133691/8 "2008-04-04T00:28:52Z")

</div>

Pete,

You do know this is in the api for form\_tag, don’t you?

Julian.

Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) VIDEO #3  
OUT APRIL 6  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [April 4, 2008, 3:42pm UTC](https://www.ruby-forum.com/t/testing-for-form-submission/133691/9 "2008-04-04T15:42:35Z")

</div>

It didn’t seem that clear to me in the API so I added another example  
here should anyone else feel the same.

Thanks Julian.
