pete
April 2, 2008, 5:24pm
1
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!
pete
April 2, 2008, 5:26pm
2
Not sure why that posted twice…
Anyhow, here’s how I call it in the controller:
@file = CSV::Reader.parse(params[:dump][:file])
pete
April 2, 2008, 5:43pm
3
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
pete
April 2, 2008, 6:00pm
4
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]
pete
April 3, 2008, 10:35am
5
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/
pete
April 3, 2008, 6:00pm
6
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?
pete
April 3, 2008, 10:09pm
7
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
April 4, 2008, 12:28am
8
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/
pete
April 4, 2008, 3:42pm
9
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.