Hi,
I posted a similar question quite recently, as all the tutorials I found
online were incorrect.
I have some basic code that works for uploading files, which I have
pasted below. It has no error checking of any kind at the moment.
Hope it helps,
Jen.
Model:
class DataFile < ActiveRecord::Base
#takes the upload object and extracts the file.
def self.save(upload)
name = upload[''upload''].original_filename
directory = "public/data/upload"
# create the file path
path = File.join(directory, name)
# write the file
File.open(path, "wb") { |f|
f.write(upload[''upload''].read) }
end
end
Controller:
class UploadController < ApplicationController
def index
#Renders a fiew for the user.
render ''upload.rhtml''
end
def create
#posts to the data model.
post = DataFile.save(params[:upload])
puts "file uploaded"
render ''upload''
end
end
View:
<h1>File Upload</h1>
<% form_for(:upload, :url=>{ :controller=>"upload",
:action=>"create"},
:html => {:multipart => true}) do |f| %>
<%= f.label :"File to upload" %>
<%= f.file_field :upload%>
<%= f.submit %>
<% end %>
On 20/05/11 15:55, Benjamin Mulaosmanovic wrote:> I am having trouble finding things online on how to do a simple file
> upload. Could you guys give me some advice? Point me in right direction.
>
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.