I'm trying to implement Paperclip in my first rails app, and I happen to be using rails 3 and mongodb with mongomapper.
I followed this guide on getting there things to all work together
Just as the blog post suggests, I've put paperclip into the config/initializers directory, I installed the gem, the gem is in the gemfile (rails 3 right), I ran the bundler.
In my user class, I've added
require 'paperclip'
When I load the app, I get the following error,
undefined method 'has_attached_file' for User:Class
The paperclip file looks like this
module Paperclip
module ClassMethods
def has_attached_file name, options = {}
include InstanceMethods
write_inheritable_attribute(:attachment_definitions, {}) if attachment_definitions.nil?
attachment_definitions[name] = {:validations => []}.merge(options)
after_save :save_attached_files
before_destroy :destroy_attached_files
define_callbacks :before_post_process, :after_post_process
define_callbacks :"before_#{name}_post_process", :"after_#{name}_post_process"
define_method name do |*args|
a = attachment_for(name)
(args.length > 0) ? a.to_s(args.first) : a
end
define_method "#{name}=" do |file|
attachment_for(name).assign(file)
end
define_method "#{name}?" do
attachment_for(name).file?
end
validates_each name, :logic => lambda {
attachment = attachment_for(name)
attachment.send(:flush_errors) unless attachment.valid?
}
end
end
module Interpolations
# Handle string ids (mongo)
def id_partition attachment, style
if (id = attachment.instance.id).is_a?(Integer)
("%09d" % id).scan(/\d{3}/).join("/")
else
id.scan(/.{3}/).first(3).join("/")
end
end
end
end
Any suggestions on what I may be doing wrong? have I got the steps right?
As of MongoMapper 0.11.0, Paperclip 2.5.2, and Rails 3.0.4, all you need is:
#your model
require 'paperclip'
class User
include MongoMapper::Document
include Paperclip::Glue
has_attached_file :avatar
key :avatar_file_name, String
end
You don't need a Paperclip initializer anymore.
I just released a gem to take care of this after the above code snippets didn't quite work for later versions of paperclip and rails.
Check out mongomapper-paperclip
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With