Greg Ruby
2006-Aug-01 20:26 UTC
[Rails] 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) %>
<p><label
for="upload_title">Title</label><br/>
<%= text_field ''upload'', ''title''
%></p>
<p><label
for="upload_description">Description</label><br/>
<%= text_area ''upload'', ''description''
%></p>
<p><b>Name:</b><br />
<%= text_field "upload", "name" %></p>
<p><b>File:</b><br />
<%= file_field "upload", "file" %></p>
<p><%= submit_tag "Save" %></p>
<%= end_form_tag %>
--
Posted via http://www.ruby-forum.com/.
John Ivanoff
2006-Aug-01 20:33 UTC
[Rails] Upload file to file system and sql insert date errors
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
------------------------------------------
On 8/1/06, Greg Ruby <rubygreg@mailinator.com>
wrote:> 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) %>
>
> <p><label
for="upload_title">Title</label><br/>
> <%= text_field ''upload'', ''title''
%></p>
>
> <p><label
for="upload_description">Description</label><br/>
> <%= text_area ''upload'',
''description'' %></p>
>
> <p><b>Name:</b><br />
> <%= text_field "upload", "name" %></p>
>
> <p><b>File:</b><br />
> <%= file_field "upload", "file" %></p>
>
> <p><%= submit_tag "Save" %></p>
>
> <%= end_form_tag %>
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
Greg Ruby
2006-Aug-01 21:00 UTC
[Rails] Re: Upload file to file system and sql insert date errors
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. -- Posted via http://www.ruby-forum.com/.
John Ivanoff
2006-Aug-02 16:29 UTC
[Rails] Re: Upload file to file system and sql insert date errors
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
On 8/1/06, Greg Ruby <rubygreg@mailinator.com>
wrote:> 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.
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>