Upload file to file system and sql insert date errors

I’m trying to use one form to upload a file to the file system
and insert a title and description field into the database.

I have no problem uploading files to the file system
using the code from
http://wiki.rubyonrails.com/rails/pages/HowtoUploadFiles

There are more fields on the form than need to be inserted into
the database, name and file.

I need to pull out these extra fields before I can insert them into the
database.

My last effort was to try and use a sql statement in the model, but
have not got that to work yet.

Any pointers?

Thank you.

Greg


model

class Upload < ActiveRecord::Base

def self.insert(upload)
“INSERT INTO ‘uploads’ VALUES
(#{upload[‘title’]},#{upload[‘description’]})”
end

def self.save(upload)
if !File.exists?("./traces/#{upload[‘name’]}")
Dir.mkdir("./traces/#{upload[‘name’]}")
end
File.open("./traces/#{upload[‘name’]}/#{upload[‘file’].original_filename}",
“wb”) { |f| f.write(upload[‘file’].read) }
end
end

contorller

class UploadController < ApplicationController
def create
@upload = Upload.new(@params[“upload”])

  upload = Upload.insert(@params["upload"])

redirect_to :action => "show"

end
end

view

<%= start_form_tag({:action => ‘create’}, :multipart => true) %>

Title
<%= text_field 'upload', 'title' %>

Description
<%= text_area 'upload', 'description' %>

Name:
<%= text_field "upload", "name" %>

File:
<%= file_field "upload", "file" %>

<%= submit_tag "Save" %>

<%= end_form_tag %>

this is code i have that works for me.

#model

def file=(incomming_file)
@temp_file = incomming_file
fields = incomming_file.original_filename.chomp.split(’.’) # gets
the filename
@title = fields[0] # with
out the extention
@filename = base_part_of(incomming_file.original_filename)
@content_type = incomming_file.content_type
self.title = fields[0]
self.filename = base_part_of(incomming_file.original_filename)
self.content_type = incomming_file.content_type.chomp

end

def base_part_of(file_name)
filename = File.basename(file_name)
filename.gsub(/[^\w._-]/, ‘’)
end

def after_save

if @temp_file
  logger.debug("saving '#{RAILS_ROOT}/aspfiles/#{@filename}'")
  File.open("#{RAILS_ROOT}/aspfiles/#{@filename}", "wb") do |f|
    f.write(@temp_file.read)
  end
end

end


Thanks John.

Are you passing any fields to the database from the
form? That’s what I’m having a problem with. My form
will end up with several fields that need to be stored
in the db at the same time the file is uploaded.

Thanks.

actaully no fields from form into db. I am uploading a file and
grabbing info from the file and intersting that into the db as well as
time stamping it.
are you doing something like select a file and you have a “title” field?

I apoligize I read you e-mail as I was running out the door. plus I’m
still learning this…

here’s my controller…

def new
@aspfile = Aspfile.new
end

def create
params[:aspfile][“created_on”] = Time.now
@aspfile = Aspfile.new(params[:aspfile])
if @aspfile.save
redirect_to :action => ‘show’, :id => @aspfile
else
render :action => ‘new’
end
end

hth

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