# Upload file to save

**URL:** <https://www.ruby-forum.com/t/upload-file-to-save/161583>\
**Category:** Rails\
**Created:** [February 27, 2009, 4:38pm UTC](https://www.ruby-forum.com/t/upload-file-to-save/161583 "2009-02-27T16:38:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![norricorp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/df705f/32.png) [@norricorp](https://www.ruby-forum.com/u/norricorp)\
**Post date:** [February 27, 2009, 4:38pm UTC](https://www.ruby-forum.com/t/upload-file-to-save/161583/1 "2009-02-27T16:38:01Z")

</div>

I am new to rails and am stuck on the above.  
I have used a mixture of code from the agile web dev with rails book  
and what I have found with google.  
I realise there are easier ways to do this with various plugins but I  
would be interested to know why my code does not work.  
The code runs and a file is created in the expected place but this  
file is zero length so something is wrong.  
Model code  
class Photo \< ActiveRecord::Base

belongs\_to :user

validates\_format\_of :content\_type,  
:with =\> /^image/,  
:message =\> “— you can only upload pictures”

def uploaded\_picture=(picture\_field)  
self.photofile = base\_part\_of  
(picture\_field.original\_filename)  
self.content\_type = picture\_field.content\_type.chomp  
self.is\_visible = 0  
@data = picture\_field.read  
end  
etc etc

Controller code  
def create  
@photo = Photo.new(params[:photo])  
filename = @photo.photofile  
directory = “public/images”  
# create the file path  
user= User.find(session[:userid])  
path = File.join(directory, user.user\_id)  
if File.exist?(path) == false  
Dir.mkdir(path)  
end  
path = File.join(directory, user.user\_id, filename)  
# write the file

# File.new(path, “wb”) { |f| f.write(params[:picture\_field].read) }

```
 File.new(path, "wb") { |f| f.write(@data) }

```

and the view  
\<% form\_for(:photo,  
:url =\> {:action =\> ‘create’},  
:html =\> { :multipart =\> true }) do |f| %\>

**Caption**  
 \<%= f.text\_field :caption %\>

 Upload your picture: \<%= f.file\_field("uploaded\_picture") %\>  

\<%= f.submit "Upload Photo" %\>

\<% end %\>

So there are obviously some basics here that I have not yet “got”.

Many thanks for looking

---

<div class="post-metadata">

**Author:** ![norricorp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/df705f/32.png) [@norricorp](https://www.ruby-forum.com/u/norricorp)\
**Post date:** [March 3, 2009, 12:49pm UTC](https://www.ruby-forum.com/t/upload-file-to-save/161583/2 "2009-03-03T12:49:40Z")

</div>

it seems you’re trying to write @data into that file. you didn’t  
initialize that variable in your controller. the only occurrence is  
inside your model. but model and controller don’t share variables  
(even if they have a similar name).

in such a case it is always helpful to debug your code. set a debugger  
and see which variables contain which values.

also: don’t try to call methods (in your model) from your view. in  
general i’d advice to stick to MVC-pattern. you could put that  
uploaded\_picture-code into your helper or just recode it.

---

<div class="post-metadata">

**Author:** ![norricorp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/df705f/32.png) [@norricorp](https://www.ruby-forum.com/u/norricorp)\
**Post date:** [March 3, 2009, 1:12pm UTC](https://www.ruby-forum.com/t/upload-file-to-save/161583/3 "2009-03-03T13:12:08Z")

</div>

for your view, if in doubt use this instead of \<%= f.file\_field  
(“uploaded\_picture”) %\>

in your controller (the line you commented out):  
File.new(path, “wb”) { |f| f.write(params[:chosen\_file].read) }

if i didn’t miss anything, this should do the trick.

---

<div class="post-metadata">

**Author:** ![norricorp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/df705f/32.png) [@norricorp](https://www.ruby-forum.com/u/norricorp)\
**Post date:** [March 6, 2009, 1:18pm UTC](https://www.ruby-forum.com/t/upload-file-to-save/161583/4 "2009-03-06T13:18:17Z")

</div>

well, you got a few errors here and there. for example:  
path = File.join(directory, user.user\_id, filename)  
i guess user doesn’t have a user\_id but an id, so try:  
path = File.join(directory, user.id, filename)

if you post your error-message, we can try to get there step by step.  
but in general it would be best if you followed some tutorials for  
your first steps.

---

<div class="post-metadata">

**Author:** ![norricorp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/df705f/32.png) [@norricorp](https://www.ruby-forum.com/u/norricorp)\
**Post date:** [March 6, 2009, 1:07pm UTC](https://www.ruby-forum.com/t/upload-file-to-save/161583/5 "2009-03-06T13:07:04Z")

</div>

I tried this and got an error.  
My stuff was based on this website -  
[Ruby on Rails - File Uploading](http://www.tutorialspoint.com/ruby-on-rails/rails-file-uploading.htm).  
I wanted a photo stored in images/user-id/ whereas this only uses  
images/. So if I remove the user-id directory, and do this, I still  
get an empty file.  
Thanks for you replies - I need to think my rails usage.
