views:

116

answers:

2

How do you handle file upload in rail without attaching them to active record ?
I just want to write the files to the disk.

Thanks,

+1  A: 

You could try using the Rails plugin Attachment_fu to handle file uploads. It allows you to save uploads to the file system instead of the database.

kchau
attachement_fu need activerecord ....
Mike
+2  A: 

If I understand correctly what you need then the most simple example would be this:

The controller:

  class UploadController < ApplicationController
  def new

  end

  def create
    name = params[:upload][:file].original_filename
    directory = "public/images/upload"
    path = File.join(directory, name)
    File.open(path, "wb") { |f| f.write(params[:upload][:file].read) }
    flash[:notice] = "File uploaded"
    redirect_to "/upload/new"
  end
end

The view:

<% flash.each do |key, msg| %>
    <%= content_tag :div, msg, :class => [key, " message"], :id => "notice_#{key}" %>
<% end %>
<% form_tag '/upload/create', { :multipart => true } do %>
    <p>
    <%= file_field_tag 'upload[file]' %>
    </p>
    <p>
     <%= submit_tag "Upload" %>
    </p>
<% end %>

This would let you upload any file without any checks or validations which in my opinion isn't that usefull.

If I would do it myself then I would use something like validatable gem or tableless gem just tableless is not supported anymore. These gems would allow you to validate what you're uploading to make it more sane.

Rytis Lukoševičius