我正在尝试在我的第一个Rails应用程序中实现Paperclip,并且恰好使用Rails 3和MongoDB与MongoMapper。
我按照此指南的建议来让它们协同工作。
就像博客文章所建议的那样,我将Paperclip放入了config/initializers目录中,安装了gem,gem在Gemfile中(Rails 3对吗),并运行了bundler。
在我的用户类中,我添加了
我按照此指南的建议来让它们协同工作。
就像博客文章所建议的那样,我将Paperclip放入了config/initializers目录中,安装了gem,gem在Gemfile中(Rails 3对吗),并运行了bundler。
在我的用户类中,我添加了
require 'paperclip'当我加载应用程序时,出现以下错误:
undefined method 'has_attached_file' for User:ClassPaperclip文件看起来像这样。
模块 Paperclip 模块 ClassMethods def has_attached_file name, options = {} 包括 InstanceMethods
如果 attachment_definitions 为空,则写入可继承属性(:attachment_definitions, {}) attachment_definitions[name] = {:validations => []}.merge(options)
在保存之后执行 :save_attached_files 在销毁之前执行 :destroy_attached_files
定义回调 :before_post_process, :after_post_process 定义回调 :"before_#{name}_post_process", :"after_#{name}_post_process"
定义方法 name do |*args| a = attachment_for(name) (args.length > 0) ? a.to_s(args.first) : a end
定义方法 "#{name}=" do |file| attachment_for(name).assign(file) end
定义方法 "#{name}?" do attachment_for(name).file? end
验证每个 name,逻辑为 lambda { attachment = attachment_for(name) attachment.send(:flush_errors) unless attachment.valid? } end end
模块 Interpolations # 处理字符串 id(mongo) def id_partition attachment, style 如果 (id = attachment.instance.id).is_a?(Integer) ("%09d" % id).scan(/\d{3}/).join("/") else id.scan(/.{3}/).first(3).join("/") end end end end
你有什么建议吗?我可能做错了什么?我的步骤正确吗?