ruby-on-rails – 在Rails中使用MongoMapper的Paperclip 3

我正在尝试在我的第一个rails应用程序中实现Paperclip,我碰巧使用rails 3和 mongodb与mongomapper.

我跟着this guide一起去做所有工作的事情

正如博客文章所说,我已将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

您不再需要Paperclip初始化程序.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...