【问题标题】:Paperclip with MongoMapper in Rails 3Rails 3 中带有 MongoMapper 的回形针
【发布时间】:2011-03-21 21:21:48
【问题描述】:

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

我关注了this guide,让他们一起工作

正如博文所暗示的,我已将回形针放入 config/initializers 目录, 我安装了 gem,gem 在 gemfile 中(rails 3 右),我运行了捆绑器。

在我的用户类中,我添加了

需要'回形针'

当我加载应用程序时,我收到以下错误,

User:Class 的未定义方法“has_attached_file”

回形针文件是这样的

模块回形针
  模块类方法
    def has_attached_file name, options = {}
      包括 InstanceMethods

      write_inheritable_attribute(:attachment_definitions, {}) 如果 attachment_definitions.nil?
      attachment_definitions[name] = {:validations => []}.merge(options)

      after_save :save_attached_files
      before_destroy :destroy_attached_files

      定义回调:before_post_process,:after_post_process
      define_callbacks :"before_#{name}_post_process", :"after_#{name}_post_process"

      定义方法名 |*args|
        a = attachment_for(名称)
        (args.length > 0) ? a.to_s(args.first) :一个
      结尾

      define_method "#{name}=" 做 |file|
        attachment_for(name).assign(file)
      结尾

      定义方法“#{name}?”做
        attachment_for(name).file?
      结尾

      validates_each 名称,:logic => lambda {
        附件 = attachment_for(名称)
        attachment.send(:flush_errors) 除非 attachment.valid?
      }
    结尾
  结尾

  模块插值
    # 处理字符串 id (mongo)
    def id_partition 附件,样式
      if (id = attachment.instance.id).is_a?(Integer)
        ("%09d" % id).scan(/\d{3}/).join("/")
      别的
        id.scan(/.{3}/).first(3).join("/")
      结尾
    结尾
  结尾
结尾

关于我可能做错的任何建议?我的步骤正确吗?

【问题讨论】:

    标签: ruby-on-rails paperclip mongomapper


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我刚刚发布了一个 gem 来解决这个问题,因为上面的代码 sn-ps 对以后版本的回形针和导轨不太适用。

      查看mongomapper-paperclip

      【讨论】:

        【解决方案3】:

        原来我两个都需要

        包括回形针 需要“回形针”

        在 user.rb 文件中。

        这导致vald的错误在哪里?没有被识别,但是我注释掉了

        #除非 attachement.valid?

        现在情况正在好转。

        【讨论】:

          【解决方案4】:

          我认为我可以使用它,但某些原始解决方案不适用于新发布的版本。此解决方案适用于 Rails 3.0.4、回形针 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
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-06-22
            • 1970-01-01
            • 1970-01-01
            • 2014-01-12
            • 1970-01-01
            • 2012-01-20
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多