Ron Phillips
2007-Feb-12 19:22 UTC
Acts As Attachment :storage=> :db_system tutorial version
This is just a tweak to Rick Olson''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 <h1>DVD Covers</h1> <ul> <% @dvd_covers.each do |dvd_cover| -%> <li><%= link_to dvd_cover.filename, :action => ''show'', :id => dvd_cover %></li> <% end -%> </ul> <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) 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---