# Strange error uploading file

**URL:** <https://discuss.rubyonrails.org/t/strange-error-uploading-file/56211>\
**Category:** rubyonrails-talk\
**Created:** [December 11, 2010, 8:31pm UTC](https://discuss.rubyonrails.org/t/strange-error-uploading-file/56211 "2010-12-11T20:31:30Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [December 11, 2010, 8:31pm UTC](https://discuss.rubyonrails.org/t/strange-error-uploading-file/56211/1 "2010-12-11T20:31:30Z")

</div>

Hello, I'm just starting to learn ruby on rails and I have a problem uploading pictures to my server.

I just want to store the url of the picture and upload its file to public/images when I create a new Picture record. I don't know where is my error and I am going crazy. I don't need it for any customer or similar, I only want to understand why it doesn't work. Please help!

I have used this tutorial: [http://programming-ut.blogspot.com/2010/01/ruby-on-rails-file-upload.html](http://programming-ut.blogspot.com/2010/01/ruby-on-rails-file-upload.html)

I have two tables:

Dog - name - weight ...

Picture - url - dog\_id

Dog has many Picture Picture belong to Dog

In picture\_controller I just call a method 'upload' of the model at creation:

def create &nbsp;&nbsp;@picture = Picture.new(params[:picture]) &nbsp;&nbsp;post = Picture.upload(params[:picture],@picture) &nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;if @picture.save &nbsp;&nbsp;&nbsp;&nbsp;format.html { redirect\_to(@picture, :notice =\> 'Created!') } &nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @picture, :status =\> :created, :location =\> @picture } &nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;format.html { render :action =\> "new" } &nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @picture.errors, :status =\> :unprocessable\_entity } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

The method code is:

class Picture \< ActiveRecord::Base &nbsp;&nbsp;belongs\_to :dog &nbsp;&nbsp;def self.upload(data,insert) &nbsp;&nbsp;&nbsp;&nbsp;fullname = data['url'].original\_filename &nbsp;&nbsp;&nbsp;&nbsp;name = File.basename(fullname) &nbsp;&nbsp;&nbsp;&nbsp;name.sub(/[^\w\.\-]/,'\_') &nbsp;&nbsp;&nbsp;&nbsp;insert.url = name &nbsp;&nbsp;&nbsp;&nbsp;directory = "/public/images" &nbsp;&nbsp;&nbsp;&nbsp;path = File.join(directory, insert.url) &nbsp;&nbsp;&nbsp;&nbsp;File.open(path, "wb") { |f| f.write(data['url'].read) } &nbsp;&nbsp;end end

And the view:

\<h1\>New picture\</h1\> \<% form\_for(@picture, :html =\> {:multipart =\> true}) do |f| %\> &nbsp;&nbsp;\<%= f.error\_messages %\> &nbsp;&nbsp;\<p\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.label :url %\>\<br /\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.file\_field :url %\> &nbsp;&nbsp;\</p\> &nbsp;&nbsp;\<p\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.label :perro\_id %\>\<br /\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.text\_field :perro\_id %\> &nbsp;&nbsp;\</p\> &nbsp;&nbsp;\<p\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.submit 'Create' %\> &nbsp;&nbsp;\</p\> \<% end %\> When I push button Create, the log shows:

Processing PicturesController#create (for 83.44.0.216 at 2010-12-11 21:16:49) [POST] &nbsp;&nbsp;Parameters: {"commit"=\>"Create", "authenticity\_token"=\>"oZTzcJa99hTRHHl9hGSTEG70cbLITHt58wkNZIMlFo4=", "picture"=\>"perro\_id"=\>"", "url"=\>#\<File:/tmp/RackMultipart20101211-28729-1ev718o-0\>}}

Errno::ENOENT (No such file or directory - /public/prueba): &nbsp;&nbsp;app/models/picture.rb:26:in `initialize' &nbsp;&nbsp;app/models/picture.rb:26:in `open'

If you have read to this point... I can only say thanks!
