ped*_*ete 3 ruby-on-rails paperclip mongomapper
我正在尝试在我的第一个rails应用程序中实现Paperclip,我碰巧使用rails 3和mongodb与mongomapper.
我按照本指南将所有事情放在一起工作
正如博客文章所暗示的那样,我将paperclip放入config/initializers目录,我安装了gem,gem在gemfile中(rails 3右边),我运行了捆绑器.
在我的用户类中,我添加了
require 'paperclip'
当我加载应用程序时,我收到以下错误,
undefined method 'has_attached_file' for User:Class
回形针文件如下所示
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
关于我可能做错的任何建议?我有正确的步骤吗?
从MongoMapper 0.11.0,Paperclip 2.5.2和Rails 3.0.4开始,您只需要:
#your model
require 'paperclip'
class User
include MongoMapper::Document
include Paperclip::Glue
has_attached_file :avatar
key :avatar_file_name, String
end
Run Code Online (Sandbox Code Playgroud)
您不再需要Paperclip初始化程序.
| 归档时间: |
|
| 查看次数: |
2772 次 |
| 最近记录: |