# Upload file to file system and sql insert date errors

**URL:** <https://www.ruby-forum.com/t/upload-file-to-file-system-and-sql-insert-date-errors/100907>\
**Category:** Rails\
**Created:** [August 1, 2006, 10:26pm UTC](https://www.ruby-forum.com/t/upload-file-to-file-system-and-sql-insert-date-errors/100907 "2006-08-01T22:26:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubygreg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rubygreg](https://www.ruby-forum.com/u/rubygreg)\
**Post date:** [August 1, 2006, 10:26pm UTC](https://www.ruby-forum.com/t/upload-file-to-file-system-and-sql-insert-date-errors/100907/1 "2006-08-01T22:26:21Z")

</div>

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](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 %\>

---

<div class="post-metadata">

**Author:** ![rubygreg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rubygreg](https://www.ruby-forum.com/u/rubygreg)\
**Post date:** [August 1, 2006, 10:38pm UTC](https://www.ruby-forum.com/t/upload-file-to-file-system-and-sql-insert-date-errors/100907/2 "2006-08-01T22:38:06Z")

</div>

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

* * *

---

<div class="post-metadata">

**Author:** ![rubygreg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rubygreg](https://www.ruby-forum.com/u/rubygreg)\
**Post date:** [August 1, 2006, 11:00pm UTC](https://www.ruby-forum.com/t/upload-file-to-file-system-and-sql-insert-date-errors/100907/3 "2006-08-01T23:00:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rubygreg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rubygreg](https://www.ruby-forum.com/u/rubygreg)\
**Post date:** [August 3, 2006, 12:52pm UTC](https://www.ruby-forum.com/t/upload-file-to-file-system-and-sql-insert-date-errors/100907/4 "2006-08-03T12:52:14Z")

</div>

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
