在Rails 3中使用MongoMapper和Paperclip

3
我正在尝试在我的第一个Rails应用程序中实现Paperclip,并且恰好使用Rails 3和MongoDB与MongoMapper。
我按照此指南的建议来让它们协同工作。
就像博客文章所建议的那样,我将Paperclip放入了config/initializers目录中,安装了gem,gem在Gemfile中(Rails 3对吗),并运行了bundler。
在我的用户类中,我添加了
require 'paperclip'
当我加载应用程序时,出现以下错误:
undefined method 'has_attached_file' for User:Class
Paperclip文件看起来像这样。
模块 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

你有什么建议吗?我可能做错了什么?我的步骤正确吗?

4个回答

6

从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的初始化程序。

1

在上述代码片段无法适用于较新版本的paperclip和rails之后,我发布了一个gem来解决这个问题。

请查看mongomapper-paperclip


0

我想我已经解决了问题,但是一些原始的解决方案在新发布的版本中无法使用。这个解决方案适用于Rails 3.0.4、paperclip 2.3.8和mongo_mapper 0.8.6:

模型:

# app/models/entry_image.rb
require 'paperclip'

class EntryImage
  include MongoMapper::Document
  include Paperclip::Glue

  has_attached_file :image, :styles => {:thumb => "25x25#"}

  key :image_file_name, String

end

初始化器

# config/initializers/mongo_paperclip.rb
# Code originally from 
# http://anlek.com/2010/06/getting-paperclip-to-work-with-mongomapper/
# Additional changes to work with newer version of paperclip
module Paperclip
  class << self
    def logger #:nodoc:
      MongoMapper.logger
    end
  end

  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 {|record|
        attachment = record.attachment_for(name)
        attachment.send(:flush_errors)
      }
    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

0

结果发现我需要两个

 
    包括Paperclip
    要求'paperclip'

在user.rb文件中。

这导致一个错误,其中vald?无法识别,但我注释了

# unless attachement.valid?

现在情况好多了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接