Need immediate help on file uploading


#1

Hi Guys

i’ll give you my model, controller(not entire one) and view that i
already
have

model

class MetroUpload < ActiveRecord::Base

def self.save(metro_upload)
File.open(“Metro_SQL/#{metro_upload[‘name’]}/metro.sql”, “w”) {
|f|
f.read(metro_upload[‘sql_file’].rewind)
f.write(metro_upload[‘sql_file’].read)
}

end

end

controller

def create

if @post = MetroUpload.save(@params["metro_upload"])
breakpoint
  flash[:notice] = 'Metro Upload was successfully created.'
  redirect_to :action => 'list'
else
  render :action => 'new'
end

end

virew

New Metro file upload

<%= form_remote_tag (:update =>“update_div”,:multipart => true,
:url =>{:action => ‘create’}) %>

<%= render :partial => ‘form’ %> # see the partial
<%= submit_tag “Upload it” %>
<%= end_form_tag %>

<%= link_to ‘Back’, :controller => ‘badger_cab_erp_admin’ %>

_form.rhtml # my partial

<%= error_messages_for ‘metro_upload’ %>

Attach your sql file

my problem is i can upload a file in this way, but there is nothing in
my
file (there is nothing in its contents)

only file will be created with the given name - zero length file

Plz any one can tell me how to fix this ( i want to upload the file with
its
contents)

Thx
Kushan


#2

Kushan J. schreef:

Hi Guys

<%= form_remote_tag (:update =>“update_div”,:multipart => true,
:url =>{:action => ‘create’}) %>
As fas as I know, uploading files doesn’t work with AJAXified forms.
Try this:

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