I''m trying to create a document upload system, where most of the code is the super class Document and just the path to file on the system is controlled by the sub classes. When I attempt to use my code, I get the following when I try to save the document. can''t dump anonymous class Class Any ideas? ** Migration ** class CreateDocuments < ActiveRecord::Migration def self.up create_table :documents do |t| t.column :type, :string t.column :name, :string t.column :file_path, :string t.column :content_type, :string t.column :created_on, :timestamp # attributes for type="AssociationDocument" t.column :association_id, :integer # attributes for type="LocationDocument" t.column :location_id, :integer end end def self.down drop_table :documents end end ** Models ** class Document < ActiveRecord::Base attr_accessor :uploaded_file def after_create if !File.exists?(File.dirname(self.file_path)) Dir.mkdir(File.dirname(self.file_path)) end if @uploaded_file.instance_of?(Tempfile) FileUtils.copy(@uploaded_file.local_path, self.file_path) else File.open(self.file_path, "wb") { |f| f.write(@uploaded_file.read) } end end def after_destroy if File.exists?(self.file_path) File.delete(self.file_path) end end def file=(uploaded_file) @uploaded_file = uploaded_file self.file_path = sanitize_filename(@uploaded_file.original_filename) self.content_type = @uploaded_file.content_type end private def new_filename(file_name) idx = file_name.rindex "." self.name.gsub(/[^\w\.\_]/, ''_'') + file_name[idx, file_name.length - idx] end def sanitize_filename(file_name) File.expand_path("#{RAILS_ROOT}/public/documents/#{new_filename(file_name)}") end end class GlobalDocument < Document validates_uniqueness_of :name, :scope => :type private def sanitize_filename(file_name) File.expand_path("#{RAILS_ROOT}/public/documents/global/#{new_filename(file_name)}") end end ** Controller ** def documents @associations = Association.find(:all, :order => [ "name" ]) @association_documents = AssociationDocument.find(:all, :include => [ "association" ]) @global_documents = GlobalDocument.find_all() @new_document = GlobalDocument.new end def save_global_document doc = GlobalDocument.new(params[:new_document]) if doc.save flash[:notice] = "<em>#{doc.name}</em> was saved" else flash[:error] = doc.errors.full_messages.join("<br />") end redirect_to :action => :documents end ** View ** documents.rhtml <%= form_tag({ :action => :save_global_document }, { :multipart => true }) %> <table class="layout_table"> <tr> <td class="right"><label for="new_document_name" class="required">Name:</label></td> <td><%= text_field :new_document, :name, :class => "fixed" %></td> </tr> <tr> <td class="right"><label for="new_document_file" class="required">File:</label></td> <td><%= file_field :new_document, :file, :class => "fixed" %></td> </tr> <tr> <td></td> <td><%= submit_tag "Upload file" %></td> </tr> </table> <%= end_form_tag %> -- Posted via http://www.ruby-forum.com/.
On 6/19/06, Stephen Gerstacker <stephen@shortround.net> wrote:> I''m trying to create a document upload system, where most of the code is > the super class Document and just the path to file on the system is > controlled by the sub classes. When I attempt to use my code, I get the > following when I try to save the document. > > can''t dump anonymous class Class > > Any ideas?Do you have a stack trace from your logs that shows which line of code is causing this? -- James
Sorry for the delay, but the Ruby Forum page is down. The stack trace shows it dies on the doc.save portion of the controller. Stephen
Here is the stack trace: C:/Ruby/lib/ruby/1.8/yaml/rubytypes.rb:6:in `to_yaml'' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/connect ion_adapters/abstract/quoting.rb:22:in `quote'' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/connect ion_adapters/mysql_adapter.rb:123:in `quote'' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb :1305:in `quote_bound_value'' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb :1287:in `replace_bind_variables'' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb :1287:in `replace_bind_variables'' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb :1276:in `sanitize_sql'' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb :1062:in `add_conditions!'' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb :1012:in `construct_finder_sql'' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb :924:in `find_every'' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb :918:in `find_initial'' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb :380:in `find'' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validat ions.rb:528:in `validates_uniqueness_of'' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validat ions.rb:302:in `validates_each'' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validat ions.rb:299:in `validates_each'' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validat ions.rb:794:in `run_validations'' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validat ions.rb:788:in `run_validations'' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validat ions.rb:752:in `valid_without_callbacks'' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/callbac ks.rb:306:in `valid?'' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validat ions.rb:723:in `save_without_transactions'' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/transac tions.rb:126:in `save'' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/connect ion_adapters/abstract/database_statements.rb:51:in `transaction'' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/transac tions.rb:91:in `transaction'' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/transac tions.rb:118:in `transaction'' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/transac tions.rb:126:in `save'' #{RAILS_ROOT}/app/controllers/enrollment_admin_controller.rb:34:in `save_global_document'' -e:4