How to upload a file

Hello all,

i am trying to upload a file through Ruby on Rails since last week. Could anyone please guide me regarding a simple upload process. Or guide me to a 'working' example/tutorial of uploading.

Waiting curiously for a respons,

Regards,

pharrukh.

the super simple version: in your view place a form:

<% form_tag(...insert your path etc...), { :multipart => true }) do %>   <label for="file">File to Upload</label><%= file_field_tag "file" %>   <%= submit_tag("upload") %> <% end %>

then you'll get this file in your action as params[:file] and can save it like this:

File.open("#{file_location}/new_file_name.xxx", 'wb') { |f| f.write(params[:file].read) }

The important bit that's easy to forget is the :multipart=>true

==> in your view new.rhtml <==

<% form_for(:something, @something, :url => somethings_path, :html => { :multipart => true }) do |f| -%>    <p> <label for="something_data">Something File</label>:<br />      <%= f.file_field 'data' %> </p>

   <%= submit_tag "Submit", :name => nil %> <% end %>

==> in the SomethingController#create action <==

     data = params[:something].delete('data') # HashWithIndifferentAccess still needs the actual key type to .delete

     params[:dump] = "data=#{data.inspect}"#; filename=#{data.original_filename}; content_type=#{data.content_type}"

     if data.blank?        flash[:error] = "No something file selected for upload"        redirect_to :action => 'new' and return      end      content = data.read      if content.blank?        flash[:error] = "Selected something file was empty"        redirect_to :action => 'new' and return      end

That should get you going.

-Rob

Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com