Testing for Form Submission?

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!

Not sure why that posted twice…

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

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

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. [email protected]

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/

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?

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]

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/

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.