Acts As Attachment :storage=> :db_system tutorial version

This is just a tweak to Rick O.'s tutorial, because his shows
file_system storage, and because I got file_system running on the first
go every time I tried, but only got db_system running after a lot of
struggling. I was never sure whether it was my db configuration or my
acts_as_attachment configuration, so I kept fiddling one, then the
other.

It took a while to realize SQL Server wasn’t EVER going to work. Once I
switched to MySQL, it wasn’t bad at all. It doesn’t look like much now.

Here’s the migration as I used it:

##db/migrate/001_create_dvd_covers.rb
class CreateDvdCovers < ActiveRecord::Migration
def self.up
create_table :dvd_covers do |t|
t.column ‘dvd_id’, :integer
t.column “content_type”, :string
t.column “filename”, :string
t.column “size”, :integer

  # used with thumbnails, always required
  t.column "parent_id",  :integer
  t.column "thumbnail", :string

  # required for images only
  t.column "width", :integer
  t.column "height", :integer

  # required for db-based files only
  t.column "db_file_id", :integer
end

# only for db-based files
create_table :db_files, :force => true do |t|
  t.column :data, :binary, :limit=>3.megabytes
end

end

def self.down
drop_table :dvd_covers

# only for db-based files
drop_table :db_files

end
end

And the /app files:

##app/models/dvd_cover.rb
class DvdCover < ActiveRecord::Base
acts_as_attachment
validates_as_attachment
belongs_to :db_file
end

##app/models/db_file.rb
class DbFile < ActiveRecord::Base
has_one :dvd_cover
end

app/views/dvd_covers/index.rhtml

DVD Covers

    <% @dvd_covers.each do |dvd_cover| -%>
  • <%= link_to dvd_cover.filename, :action => 'show', :id => dvd_cover %>
  • <% end -%>

<%= link_to 'New', :action => 'new' %>

app/views/dvd_covers/new.rhtml

New DVD Cover

<% form_for :dvd_cover, :url => { :action => 'create' }, :html => { :multipart => true } do |f| -%>

<%= f.file_field :uploaded_data %>

<%= submit_tag :Create %>

<% end -%>

##(NOTE: app/views/dvd_covers/show.rhtml isn’t needed for this version,
since it’s a “send_data” in the controller.)

app/controllers/dvd_covers_controller.rb

class DvdCoversController < ApplicationController
def index
@dvd_covers = DvdCover.find(:all)
end

def new
@dvd_cover = DvdCover.new
end

def show
@dvd_cover = DvdCover.find(params[:id])
send_data @dvd_cover.db_file.data, :filename => @dvd_cover.filename,
:type => @dvd_cover.content_type, :disposition => ‘inline’
end

def create
@dvd_cover = DvdCover.create! params[:dvd_cover]
redirect_to :action => ‘show’, :id => @dvd_cover
rescue ActiveRecord::RecordInvalid
render :action => ‘new’
end
end

Now, if your database is configured properly, that should work. MySQL
only needed one tweak: on Windows, I went into C:\Program
Files\MySQL\MySQL Server 5.0\my.ini and added a line to the [mysqld]
section – max_allowed_packet=32M. Restarted MySQL and it just worked.

I never got SQL Server to work at all, so if you know the secret, post
it for me, please.

Ron

Thanks for the post Ron. I finally got ActsAsAttachment working on my
local machine with db storage.

Ben

On Feb 12 2007, 11:22 am, Ron P. <rails-mailing-l…@andreas-

Hi,

I am trying to upload an image file, in MySql. When I try to upload it’s
not throwing any error. But in the DB, it is always getting saved as
"ÿØÿà " in the Text. It is not getting saved as image.

When I tried to read the image and send(using send_data method), it
shows “No preview available”.

I tried with both acts_as_attachment and attachment_fu. Any guess what
the mistake I have made?

Thanks in advance…

I have no experience with attachment_fu or acts_as_attachment but I
setup image uploads in an app yesterday using paperclip. The whole
process took about 10 minutes but I already had imagemagick installed
so that saved me some time.

Chris B