# Picture upload

**URL:** <https://www.ruby-forum.com/t/picture-upload/173895>\
**Category:** Rails\
**Created:** [August 17, 2009, 9:38pm UTC](https://www.ruby-forum.com/t/picture-upload/173895 "2009-08-17T21:38:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![maniga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@maniga](https://www.ruby-forum.com/u/maniga)\
**Post date:** [August 17, 2009, 9:38pm UTC](https://www.ruby-forum.com/t/picture-upload/173895/1 "2009-08-17T21:38:23Z")

</div>

Hi everybody!

I’m new in programming Ruby on Rails, and I have a big problem with a  
picture upload. I like to save thumbnails in the directory structure and  
save the image name in the database.

So that’s what I’ve done:

new.html.erb

 Untitled Document 
# Post new Challenge
 \<%= form\_tag({:action =\> :create}, {:multipart =\> true}) %\> 

Challenge Title:

 \<%= text\_field :challenge, :title%\>   

Description:

 \<%= text\_field :challenge, :description%\>  

Challenge Thumbnail:

 \<%= file\_field :challenge, :thumbnail%\> \<%= submit\_tag 'create' %\> \<% if flash[:error]%\> 

\<%= flash[:error]%\>

 \<% end %\> \<%= link\_to 'Back', {:action =\> 'list'} %\> 

challenges\_controller.rb

def new  
@challenge = Challenge.new  
end

def create  
@challenge = Challenge.new params[:challenge]

```
if !@challenge.valid?
  flash.now[:error] = 'Alles muss angegeben werden!'
  render :action => 'new'
else if !@challenge.save_thumbnail
  flash.now[:error] = 'Fehler beim hochladen des thumbnails!'
  render :action => 'new'
else
  @challenge.save
  redirect_to(:action => :list)
end
end

```

end

challenge.rb

class Challenge \< ActiveRecord::Base  
belongs\_to :user  
IMAGE\_DIR = ‘/images/upload’  
THUMBNAIL\_DIR = IMAGE\_DIR + ‘/thumbnails’

def thumbnail= (thumbnail\_field)  
if thumbnail\_field.size \> 0  
@thumbnail = thumbnail\_field  
self.thumbnail = thumbnail\_field.original\_filename  
end  
end

def save\_thumbnail  
if !save\_uploaded\_file(@thumbnail, THUMBNAIL\_DIR,  
self.thumbnail\_file)  
return false  
end  
end

def save\_uploaded\_file(fileobj, filepath, filename)  
complete\_path = RAILS\_ROOT + ‘/public/’ + filepath  
FileUtils.mkdir\_p(complete\_path) unless File.exists?(complete\_path)  
begin  
filename = unique\_and\_proper\_filename filename  
f = File.open(complete\_path + ‘/’ + filename, ‘wb’)  
f.write(fileobj.read)  
rescue  
return false  
ensure  
f.close unless f.nil?  
end  
end

def unique\_and\_proper\_filename(filename)  
Time.now.to\_i.to\_s + ‘\_’ + File.basename(filename)  
end  
end

I’m getting the error message: NoMethodError in  
ChallengesController#create  
undefined method `original\_filename’ for “DSC00672.JPG”:String

I’ve tried everything, but I cant get it run correctly.

So please help!

Thanks, Manuel

---

<div class="post-metadata">

**Author:** ![maniga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@maniga](https://www.ruby-forum.com/u/maniga)\
**Post date:** [August 17, 2009, 9:45pm UTC](https://www.ruby-forum.com/t/picture-upload/173895/2 "2009-08-17T21:45:55Z")

</div>

Since you’re new to RoR, why not use a plugin like Paperclip? It does  
exactly what you’re trying to do and much more…should save you a lot  
of time.

[http://www.thoughtbot.com/projects/paperclip](http://www.thoughtbot.com/projects/paperclip)

> **[\#134 Paperclip - RailsCasts](http://railscasts.com/episodes/134-paperclip)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

---

<div class="post-metadata">

**Author:** ![maniga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@maniga](https://www.ruby-forum.com/u/maniga)\
**Post date:** [August 18, 2009, 1:21am UTC](https://www.ruby-forum.com/t/picture-upload/173895/3 "2009-08-18T01:21:33Z")

</div>

Lee S. wrote:

> Since you’re new to RoR, why not use a plugin like Paperclip? It does  
> exactly what you’re trying to do and much more…should save you a lot  
> of time.
> 
> [http://www.thoughtbot.com/projects/paperclip](http://www.thoughtbot.com/projects/paperclip)
> 
> [#134 Paperclip - RailsCasts](http://railscasts.com/episodes/134-paperclip)

Thanks for your answer.

Ok now I have a problem with paperclip. Is it possible that it doesn’t  
work under windows vista? I installed the plugin and tried the example  
from the paperclip page. There are entries in the db but with null  
values in the image related columns and the pictures are not in the  
puplic directory.

I’m getting crazy!

Here is what I’ve done:

challenge.rb

class Challenge \< ActiveRecord::Base  
belongs\_to :user  
has\_attached\_file :thumbnail, :styles =\> {:thumb =\> ‘100x100\>’}

end

new.html.erb

# Post new Challenge
 \<%form\_tag :action =\> 'create', :multipart =\> true do %\> 

Challenge Title:

 \<%= text\_field 'challenge', 'title'%\>   

Description:

 \<%= text\_field 'challenge', 'description'%\>  

Challenge Thumbnail:

 \<%= file\_field 'challenge', :thumbnail%\> \<%= submit\_tag 'Create' %\> \<%end%\>

challenges\_controller.rb

def create  
@challenge = Challenge.create params[:challenge]

```
if !@challenge.valid?
  flash.now[:error] = 'Bitte fÃ¼llen sie alle Felder aus!'
  render :action => :new

```

else if !@challenge.save  
flash.now[:error] = ‘Es trat ein Fehler beim speichern auf!’  
render :action =\> :new  
else  
redirect\_to :action =\> ‘list’  
end  
end

end

---

<div class="post-metadata">

**Author:** ![maniga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@maniga](https://www.ruby-forum.com/u/maniga)\
**Post date:** [August 18, 2009, 2:50am UTC](https://www.ruby-forum.com/t/picture-upload/173895/4 "2009-08-18T02:50:47Z")

</div>

heimdull wrote:

> Do you have something like ImageMagick installed? I don’t use and  
> haven’t used windows for a while but here is an article that might  
> help you out…
> 
> [http://thewebfellas.com/blog/2008/11/2/goodbye-attachment\_fu-hello-paperclip](http://thewebfellas.com/blog/2008/11/2/goodbye-attachment_fu-hello-paperclip)
> 
> On Aug 17, 4:21ï¿½pm, Mane M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Thanks. Yes I have ImageMagick installed. The problem is solved. I had  
to use the form\_for helper instead of the form\_tag.

I’m so happy now it works!

---

<div class="post-metadata">

**Author:** ![maniga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@maniga](https://www.ruby-forum.com/u/maniga)\
**Post date:** [August 18, 2009, 11:52am UTC](https://www.ruby-forum.com/t/picture-upload/173895/5 "2009-08-18T11:52:48Z")

</div>

I have one other question about Paperclip. Where are the pictures saved  
by default? (Windows machine)

---

<div class="post-metadata">

**Author:** ![maniga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@maniga](https://www.ruby-forum.com/u/maniga)\
**Post date:** [August 18, 2009, 2:28am UTC](https://www.ruby-forum.com/t/picture-upload/173895/6 "2009-08-18T02:28:40Z")

</div>

Do you have something like ImageMagick installed? I don’t use and  
haven’t used windows for a while but here is an article that might  
help you out…

[http://thewebfellas.com/blog/2008/11/2/goodbye-attachment\_fu-hello-paperclip](http://thewebfellas.com/blog/2008/11/2/goodbye-attachment_fu-hello-paperclip)

On Aug 17, 4:21 pm, Mane M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![maniga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@maniga](https://www.ruby-forum.com/u/maniga)\
**Post date:** [August 18, 2009, 1:52pm UTC](https://www.ruby-forum.com/t/picture-upload/173895/7 "2009-08-18T13:52:53Z")

</div>

thanks, next time i will rtfm

---

<div class="post-metadata">

**Author:** ![maniga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@maniga](https://www.ruby-forum.com/u/maniga)\
**Post date:** [August 18, 2009, 1:16pm UTC](https://www.ruby-forum.com/t/picture-upload/173895/8 "2009-08-18T13:16:04Z")

</div>

On 18 Aug 2009, at 11:52, Mane M. wrote:

> I have one other question about Paperclip. Where are the pictures  
> saved  
> by default? (Windows machine)

From the docs:

# \* +url+: The full URL of where the attachment is publically

accessible. This can just  
# as easily point to a directory served directly through Apache  
as it can to an action  
# that can control permissions. You can specify the full domain  
and path, but usually  
# just an absolute path is sufficient. The leading slash _must_  
be included manually for  
# absolute paths. The default value is  
# “/system/:attachment/:id/:style/:filename”. See  
# Paperclip::Attachment#interpolate for more information on  
variable interpolaton.  
# :url =\> “/:class/:attachment/:id/:style\_:filename”  
# :url =\> “[http://some.other.host/stuff/:class/:id\_:extension](http://some.other.host/stuff/:class/:id_:extension)”  
# \* +default\_url+: The URL that will be returned if there is no  
attachment assigned.  
# This field is interpolated just as the url is. The default  
value is  
# “/:attachment/:style/missing.png”  
# has\_attached\_file :avatar, :default\_url =\> “/images/  
default\_:style\_avatar.png”  
# User.new.avatar\_url(:small) # =\> “/images/  
default\_small\_avatar.png”
