Hi!
I'm on Windows, Ruby 1.8.6, Rails 2.2.2; I tried to apply your solution for attachment_fu and still have the "size zero" error; Here is my model:
class Document < ActiveRecord::Base
has_one :db_file, :dependent=>:destroy
has_attachment :size=> 0.kilobytes..10.megabytes
validates_as_attachment
end
Migrations:
class CreateDocuments < ActiveRecord::Migration
def self.up
create_table :documents do |t|
t.string :filename
t.string :content_type
t.integer :size
t.integer :db_file_id t.timestamps
end
end
def self.down
drop_table :documents
end
end
class CreateDbFiles < ActiveRecord::Migration
def self.up
create_table :db_files do |t|
t.binary :data
t.timestamps
end
end
def self.down
drop_table :db_files
end
end
Controller:
class DocumentsController < ApplicationController def load_file
document = Document.find(params[:id])
db_file = document.db_file
file_data = db_file.data
send_data(file_data, :type => document.content_type, :filename => document.filename)
end
def new
@document = Document.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @document }
end
end
def create
@document = Document.new(params[:document])
respond_to do |format|
if @document.save
flash[:notice] = 'Document was successfully created.'
format.html { redirect_to(documents_url) }
format.xml { render :xml => @document, :status => :created, :location => @document }
else
format.html { render :action => "new" }
format.xml { render :xml => @document.errors, :status => :unprocessable_entity }
end
end
end
View to create a new Document:
<h1>New document</h1><% form_for(@document, :html => { :multipart => true }) do |f| %>
<%= f.error_messages %>
<%= f.file_field :uploaded_data %>
<p>
<%= f.submit "Create" %>
</p>
<% end %>
<%= link_to 'Back', documents_path %>
In vendor\plugins\attachment_fu\lib\technoweenie\attachment_fu.rb:
def uploaded_data=(file_data)
return nil if file_data.nil? || file_data.size == 0
self.content_type = file_data.content_type
self.filename = file_data.original_filename if respond_to?(:filename)
if file_data.is_a?(StringIO)
file_data.rewind
self.temp_data = file_data.read
else
self.temp_path = file_data.path
end
end
def set_size_from_temp_path
self.size = File.size(temp_path) if save_attachment? && (self.size == 0 || self.size.nil?)
end
When I tries to save a PDF file of 2.4 Mb, it was truncated to 25 kB and it was saved its size set to zero. So when I tried to download it and open - no way ! Any idea?