Forum: Ruby on Rails Acts As Attachment :storage=> :db_system tutorial version

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ron P. (Guest)
on 2007-02-12 21:22
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

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:

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

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

  def self.down
    drop_table :dvd_covers

    # only for db-based files
    drop_table :db_files

And the /app files:

class DvdCover < ActiveRecord::Base
  belongs_to :db_file

class DbFile < ActiveRecord::Base
  has_one :dvd_cover

## app/views/dvd_covers/index.rhtml
<h1>DVD Covers</h1>
<% @dvd_covers.each do |dvd_cover| -%>
  <li><%= link_to dvd_cover.filename, :action => 'show', :id =>
dvd_cover %></li>
<% end -%>
<p><%= link_to 'New', :action => 'new' %></p>

## app/views/dvd_covers/new.rhtml
<h1>New DVD Cover</h1>
<% form_for :dvd_cover, :url => { :action => 'create' }, :html => {
:multipart => true } do |f| -%>
  <p><%= f.file_field :uploaded_data %></p>
  <p><%= submit_tag :Create %></p>
<% 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)

  def new
    @dvd_cover =

  def show
    @dvd_cover = DvdCover.find(params[:id])
    send_data, :filename => @dvd_cover.filename,
    :type => @dvd_cover.content_type, :disposition => 'inline'

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

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.

Ben (Guest)
on 2008-02-02 00:56
(Received via mailing list)
Thanks for the post Ron.  I finally got ActsAsAttachment working on my
local machine with db storage.


On Feb 12 2007, 11:22 am, Ron P. <rails-mailing-l...@andreas-
Karthi k. (Guest)
on 2008-08-04 10:57

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...
Chris B. (Guest)
on 2008-08-04 16:49
(Received via mailing list)
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
This topic is locked and can not be replied to.