# File Uploading codes

**URL:** <https://www.ruby-forum.com/t/file-uploading-codes/120054>\
**Category:** Ruby\
**Created:** [November 13, 2007, 12:01pm UTC](https://www.ruby-forum.com/t/file-uploading-codes/120054 "2007-11-13T12:01:59Z")\
**Posts on this page:** 3\
**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 13, 2007, 12:01pm UTC](https://www.ruby-forum.com/t/file-uploading-codes/120054/1 "2007-11-13T12:01:59Z")

</div>

I used the following codes found in the net to use to write a file to  
upload the files. down are the codes i have used. but still i did not  
able to get the correct coding. so if somebody know where i have gone  
wrong pls reply.

###################### IN the View #############################3

**Name:**  
 \<%= text\_field "person", "name" %\>

**Picture:**

#################The controller:##########################

class AddressbookController \< ApplicationController  
def new  
# not really needed since the template doesn’t rely on any data  
end

def create  
post = Post.save(@params[“person”])  
# Doesn’t this mean post is a File object?  
# post.id is a bad idea in this case…

```
redirect_to :action => "show", :id => post.id

```

end  
end

######### the model ###################

class Post \< ActiveRecord::Base  
def self.save(person)  
f = File.new(“pictures/#{person[‘name’]}/picture.jpg”, “wb”)  
f.write params[:picture].read  
f.close  
end

in the uploading it gives a error like this

undefined local variable or method `params’ for Post:Class

so i did some modification as  
f.write params[:picture].read  
line to  
f.write (@params[:picture].read)

so the code woke properly, but still it gives a error saying

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.[]

SO IF SOMEBODY KNOW HOW TO OVER COME THIS PROBLEM PLS 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 13, 2007, 12:19pm UTC](https://www.ruby-forum.com/t/file-uploading-codes/120054/2 "2007-11-13T12:19:40Z")

</div>

Nadeesha M. wrote:

> ######### the model ###################
> 
> class Post \< ActiveRecord::Base  
> def self.save(person)  
> f = File.new(“pictures/#{person[‘name’]}/picture.jpg”, “wb”)  
> f.write params[:picture].read  
> You’ll probably get a better response over on the Rails list, but I  
> suspect this is your problem - the params hash isn’t in scope in  
> ActiveRecord descendants. You should have more luck if you define your  
> method as def self.save(person, picture), and call it in you controller  
> as Post.save(params[:person], params[:picture]).

\> SO IF SOMEBODY KNOW HOW TO OVER COME THIS PROBLEM PLS REPLY NO NEED TO SHOUT! :-)

---

<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 13, 2007, 12:37pm UTC](https://www.ruby-forum.com/t/file-uploading-codes/120054/3 "2007-11-13T12:37:35Z")

</div>

Hi Alex Y.

Thanks a lot for the advice…

thanks a lot

🙂 🙂 🙂 🙂 🙂 🙂 🙂
