Forum: Ruby on Rails Need immediate help on file uploading

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Kushan J. (Guest)
on 2007-03-21 11:00
(Received via mailing list)
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

<div id="update_div">
<h1>New Metro file upload</h1>

<%= 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' %>
</div>


_form.rhtml # my partial

<%= error_messages_for 'metro_upload' %>

<!--[form:metro_upload]-->


<p><label for="metro_upload_file">Attach your sql file</label><br/>
<!--<%= file_field 'metro_upload', 'metroSQL'  %></p>-->
<input type="file" name="metro_upload[sql_file]" />
</p>


<!--[eoform:metro_upload]-->

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
nemo (Guest)
on 2007-03-21 11:03
(Received via mailing list)
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 }) %>
This topic is locked and can not be replied to.