画像をバイナリで保存する

さて、DBの問題も片付いたわけでして
http://weblog.techno-weenie.net/articles/acts_as_attachment
ここをみながら act_as_attachmentで画像をバイナリでDBに簡単にアップする方法を調査

とりあえずプラグインをインストール>ジェネレートで photoのmodelを作成

./script/plugin source http://svn.techno-weenie.net/projects/plugins
./script/plugin install acts_as_attachment
./script/generate attachment_model photo

ざっくりDBを準備してmigrate
前の記事の罠もしっかり対処

class CreatePhotos < ActiveRecord::Migration
  def self.up
    create_table "photos", :force => true do |t|
     t.column "content_type", :string
     t.column "filename",     :string
     t.column "size",         :integer
     t.column "parent_id",    :integer
     t.column "thumbnail",    :string
     t.column "width",        :integer
     t.column "height",       :integer
     t.column "db_file_id",   :integer
     t.column "datetime",     :datetime
   end
   create_table :db_files, :force => true do |t|
     t.column :data, :binary, :limit => 1.megabyte
   end   
  end

  def self.down
    drop_table :photos    
    drop_table :db_files
  end
end

とりあえずView。multipartをtrueにする必要あり

(=== View ===)
<% form_tag({:action =>'img_upload'}, {:name=>'album', :multipart => true}) do %>
  <%= file_field :photo, :uploaded_data %>
  <%= submit_tag "登録" %>
<% end %>

Controllerは受け取ってそのままセーブ!

(=== Controller ===)
  def img_upload
    @photo = Photo.new(params[:photo])
    @photo.save    
    render :action => 'list'
  end

以上でしたwww
なんてらくてぃん