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 %>