# Problem in file uploading

**URL:** <https://www.ruby-forum.com/t/problem-in-file-uploading/120161>\
**Category:** Rails\
**Created:** [November 14, 2007, 8:45am UTC](https://www.ruby-forum.com/t/problem-in-file-uploading/120161 "2007-11-14T08:45:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed8c4c/32.png) [@nad](https://www.ruby-forum.com/u/nad)\
**Post date:** [November 14, 2007, 8:45am UTC](https://www.ruby-forum.com/t/problem-in-file-uploading/120161/1 "2007-11-14T08:45:29Z")

</div>

i wote a code to do file uploading with the hep given in documents in  
the net.  
but i came across some problems.

in the View section the form that was wrote in th normal HTML was  
working 100% perfect and it uplodes the files also.

but when i edited that in the rails way (u can see the coding in the  
second form tag), when i goes to upload the file it give a error as this

" You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.read"

so if any body know where i have gone wrong ???

######### wrote in the controller page #######################33

def create

```
f = File.new("./uploads/data.doc", "wb");
f.write (@params["upload_file"].read);
f.close;

render_action 'success'

```

end

#################### wrote in the view ###################

\<% form\_for :form\_file, :url =\> { :action =\> “create” }, :html =\> {  
:multipart =\> true } do |g| %\>

\<%= g.file\_field :upload\_file %\>

\<%= submit\_tag :Up\_load\_now %\>

\<% end %\>

---

<div class="post-metadata">

**Author:** ![nad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed8c4c/32.png) [@nad](https://www.ruby-forum.com/u/nad)\
**Post date:** [November 14, 2007, 10:28am UTC](https://www.ruby-forum.com/t/problem-in-file-uploading/120161/2 "2007-11-14T10:28:32Z")

</div>

On Nov 14, 2:45 pm, Nadeesha M. \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> " You have a nil object when you didn’t expect it!  
> The error occurred while evaluating nil.read"
> 
> so if any body know where i have gone wrong ???

Form\_for is for object (when you pass activerecord object to view).  
Use form\_tag instead.

---

<div class="post-metadata">

**Author:** ![nad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed8c4c/32.png) [@nad](https://www.ruby-forum.com/u/nad)\
**Post date:** [November 14, 2007, 10:30am UTC](https://www.ruby-forum.com/t/problem-in-file-uploading/120161/3 "2007-11-14T10:30:45Z")

</div>

> ```
> f.write (@params["upload_file"].read);
> 
> ```

it’s params[“upload\_file”] not @params

---

<div class="post-metadata">

**Author:** ![nad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed8c4c/32.png) [@nad](https://www.ruby-forum.com/u/nad)\
**Post date:** [November 14, 2007, 11:14am UTC](https://www.ruby-forum.com/t/problem-in-file-uploading/120161/4 "2007-11-14T11:14:10Z")

</div>

Thorsten M. wrote:

> > ```
> > f.write (@params["upload_file"].read);
> > 
> > ```
> 
> it’s params[“upload\_file”] not @params

thanks for the reply.

---

<div class="post-metadata">

**Author:** ![nad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed8c4c/32.png) [@nad](https://www.ruby-forum.com/u/nad)\
**Post date:** [November 14, 2007, 11:17am UTC](https://www.ruby-forum.com/t/problem-in-file-uploading/120161/5 "2007-11-14T11:17:19Z")

</div>

Akbar H. wrote:

> On Nov 14, 2:45 pm, Nadeesha M. \<rails-mailing-l…@andreas-  
> [s.net](http://s.net)\> wrote:
> 
> > " You have a nil object when you didn’t expect it!  
> > The error occurred while evaluating nil.read"
> > 
> > so if any body know where i have gone wrong ???
> 
> Form\_for is for object (when you pass activerecord object to view).  
> Use form\_tag instead.

sort it out the problem thanks.

by the way do u guys know any command to check the file format.

eg like .doc, .pdf .jpg.

---

<div class="post-metadata">

**Author:** ![nad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed8c4c/32.png) [@nad](https://www.ruby-forum.com/u/nad)\
**Post date:** [November 16, 2007, 3:12am UTC](https://www.ruby-forum.com/t/problem-in-file-uploading/120161/6 "2007-11-16T03:12:50Z")

</div>

Nadeesha M. wrote:

> Akbar H. wrote:
> 
> > On Nov 14, 2:45 pm, Nadeesha M. \<rails-mailing-l…@andreas-  
> > [s.net](http://s.net)\> wrote:
> > 
> > > " You have a nil object when you didn’t expect it!  
> > > The error occurred while evaluating nil.read"
> > > 
> > > so if any body know where i have gone wrong ???
> > 
> > Form\_for is for object (when you pass activerecord object to view).  
> > Use form\_tag instead.
> 
> sort it out the problem thanks.
> 
> by the way do u guys know any command to check the file format.
> 
> eg like .doc, .pdf .jpg.

I think you can use

params[“upload\_file”].content\_type to check the file format

eg.

params[“upload\_file”].content\_type.chomp !~ /^image/

to check the upload file is image.
