Upload Image To Database
// Code To Upload an image to a database and then update its contents.
# Create an ew classified and attach an image with it. def create image_types = ["image/jpeg", "image/pjpeg", "image/gif","image/png", "image/x-png"] @categories = Category.find(:all) @classified = Classified.new(params[:classified]) @classified.user = session[:user] unless params[:classified][:picture].blank? if (image_types.include?params[:classified][:picture].content_type.chomp) @classified.picture = params[:classified][:picture].read else @classified.errors.add(:picture, "Photo doesn't seem to be JPG, GIF, or PNG. please ensure it is a valid image file.") render :action => 'new' return end end if @classified.save redirect_to :action => 'list' else render :action => 'new' end end # Update Method So user can change the image associated with their classified def update image_types = ["image/jpeg", "image/pjpeg", "image/gif", "image/png", "image/x-png"] @classified = Classified.find(params[:id]) @categories = Category.find(:all) if @classified.update_attributes(params[:classified]) unless params[:classified][:picture].blank? if (image_types.include?params[:classified][:picture].content_type.chomp) @classified.picture = params[:classified][:picture].read else @classified.errors.add(:picture, " doesn't seem to be JPG, GIF, or PNG. please ensure it is a valid image file.") render :action => 'edit' return end end flash[:notice] = 'Classified was successfully updated.' redirect_to :action => 'show', :id => @classified else render :action => 'edit' end end