Dear Diary,
Today I added an image upload plugin called attachment_fu. It worked great! The only thing that wasn’t provided for me out-of-the-box was the ability to show an image stored in the database.
The remedy for this was a new controller (actually, another sub-resource to posts), with the following method:
def show
@content_image = @post.content_images.find(params[:id])
headers['Content-Length'] = @content_image.size
send_data @content_image.db_file.data,
:type => @content_image.content_type,
:disposition => 'inline',
:filename => @content_image.filename
end
I’d like to point out one gotcha to be picked up by search engines: the README for attachment_fu simply says that you should create a migration for a model called DbFile with a field :data of type :binary. The problem is that the default for MySQL is to use the BLOB type, which has a limit of 64k. You need to use a larger database type, such as MEDIUMBLOB or LONGBLOB. This is accomplished by adding a :limit parameter to the migration:
create_table :db_files do |t|
t.binary :data, :limit => 10.megabytes # here it is
t.timestamps
end
Setting a larger :limit like that will cause a larger database type to be used.
Now, the current implementation of attaching an image to a post is really limited. If there is an image, it is shown at the top of the post, right-aligned. That’s it. No checking for dimensions, alt texts or anything like that. Also no way to tag where in the post the image should be displayed. This will (probably…?) be fixed in future versions!