Create FileColumn Image without a From

Hey,

im creating a new bookmark with a form and i’m uploading an image with
File Column. This works well!

But another function is to copy this bookmark. That means it will be
created a new Object, with an new Id. All the other attributes should
be copied from the original. That works, but not with the “image”
attribute. Thats where the fileColumn Image is placed. Im getting the
error:

'Do not know how to handle a string with value ‘/path/Bild_4.jpg’

I think fileColumn expects an File, and not an string url. But i don’t
know to solve this Problem :frowning:

Here is my Code:

@bookmark = Bookmark.new(:title => oldBookmark.title,
:url => oldBookmark.url,
:description => oldBookmark.description,
:confirmed => 0,
:user =>User.find(session[:user]),
:image => oldBookmark.image )

@bookmark.save

Maybe someone has an idea? Thanks a lot!

I’m not sure what you mean fully, but I think you mean save an item from
the
FS or URL…not uploaded via a form

here is an example from when I used a FileColumn to upload an image to
my
database using a URL (it is 2 years old and very ugly code)

essentially in your case just treat your string as I do the URL

in environment.rb have:
require ‘ftools’
require ‘fileutils’

and in your controller something like

raise if not orig =~
/^(http|https)://[a-z0-9]+([-.]{1}[a-z0-9]+).[a-z]{2,5}(([0-9]{1,5})?/.)?$/ix

   logger.error "???????????? VALUE >> #{orig}"
   response = false
      #if good url format, get the file
      tn = open(orig)
      filename = orig.split("/")

      tp = tn.path
      nf = File.new(tp)
      nf_path = File.split(nf.path)

      f = "#{nf_path[0]}" << "/" << "#{filename.last}"

      #dreamhost is case sensitive, this is for URL's only
      f = f.downcase
      logger.error "FFFFFFFFFFF  #{f}"

      FileUtils.touch f

      #sleep 2
      ff = File.new("#{f}")
      FileUtils.copy nf.path, ff.path

      @tile.img_name = File.new(ff.path) #gold

   rescue
      if response == false
        logger.error "???????????? BAD URL >> #{orig}"
      end
   end
 else
  #if orig is empty
  logger.error "ZZZZZZZZZZZZZZZZZZZ NO URL >> #{orig}"
 end

#if both img fields are empty: upload a default img (for now)
if @tile.img_name.nil?
  #@tile.img_name = File.new(RAILS_ROOT + 

“/public/images/rails.png”)
#gold default if they do not upload a file
logger.error “??? no FILE”
if @tile.img_path.empty? or response == false
@tile.img_name = File.new(RAILS_ROOT +
“/public/images/default.jpg”)
#gold default if they do not upload a file
logger.error “??? no URL”
end
else
#params[:tile][:img_name].path.downcase
end

if @tile.save
#email admin about new tile
MailMe::deliver_new_tile(@session[:user], @tile.id);

    @ts_e.save
    if session[:user].login == "anonymous"
      redirect_to :controller => '/user/logout'
    else
      redirect_to :controller => '/user/account/'
    end
  else
    render :action => 'index'  #should go back to upload_tile
  end

checkout working example at www.blogsaic.com/tile

no login required

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