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 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <h1>Post new Challenge</h1> <%= form_tag({:action => :create}, {:multipart => true}) %> <p>Challenge Title: </p> <%= text_field :challenge, :title%> <br/> <p>Description: </p> <%= text_field :challenge, :description%><br/> <p>Challenge Thumbnail: </p> <%= file_field :challenge, :thumbnail%> <%= submit_tag ''create'' %> </form> <% if flash[:error]%> <div id="error"><p><%= flash[:error]%></p></pY></div> <% end %> <%= link_to ''Back'', {:action => ''list''} %> </body> </html> 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 -- Posted via http://www.ruby-forum.com/.
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 Smith 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-paperclipThanks 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 <h1>Post new Challenge</h1> <%form_tag :action => ''create'', :multipart => true do %> <p>Challenge Title: </p> <%= text_field ''challenge'', ''title''%> <br/> <p>Description: </p> <%= text_field ''challenge'', ''description''%><br/> <p>Challenge Thumbnail: </p> <%= 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 -- Posted via http://www.ruby-forum.com/.
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 Maniga <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Lee Smith 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 > > <h1>Post new Challenge</h1> > <%form_tag :action => ''create'', :multipart => true do %> > <p>Challenge Title: </p> > <%= text_field ''challenge'', ''title''%> <br/> > <p>Description: </p> > <%= text_field ''challenge'', ''description''%><br/> > <p>Challenge Thumbnail: </p> > <%= 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 > -- > Posted viahttp://www.ruby-forum.com/.
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 Maniga <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>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! -- Posted via http://www.ruby-forum.com/.
I have one other question about Paperclip. Where are the pictures saved by default? (Windows machine) -- Posted via http://www.ruby-forum.com/.
On 18 Aug 2009, at 11:52, Mane Maniga 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" --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---