当前位置 : 主页 > 编程语言 > ruby >

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

来源:互联网 收集:自由互联 发布时间:2021-06-23
我正在尝试在我的第一个rails应用程序中实现Paperclip,我碰巧使用rails 3和 mongodb与mongomapper. 我跟着this guide一起去做所有工作的事情 正如博客文章所说,我已将paperclip放入config / initializer
我正在尝试在我的第一个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初始化程序.

网友评论