Picture upload

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 [email protected]?
  flash.now[:error] = 'Alles muss angegeben werden!'
  render :action => 'new'
else if [email protected]_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

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://railscasts.com/episodes/134-paperclip

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://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 [email protected]?
  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

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

On Aug 17, 4:21�pm, Mane M. [email protected]

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!

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

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

On Aug 17, 4:21 pm, Mane M. [email protected]

thanks, next time i will rtfm

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
# * +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”

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs