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 %>
File to Upload<%= 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) }
On Mar 13, 2008, at 9:59 AM, pharrukh. wrote:
pharrukh.
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| -%>
Something File:
<%= f.file_field 'data' %>
<%= 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 B. http://agileconsultingllc.com
[email protected]