Rails 3 中使用 MongoMapper 的回形针

发布于 2024-09-11 20:46:40 字数 1802 浏览 3 评论 0原文

我正在尝试在我的第一个 Rails 应用程序中实现 Paperclip,并且我碰巧使用 Rails 3 和 mongodb 以及 mongomapper。

我按照本指南让所有事情一起

工作正如博客文章所示,我已将回形针放入 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

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

I'm trying to implement Paperclip in my first rails app, and I happen to be using rails 3 and mongodb with mongomapper.

I followed this guide on getting there things to all work together

Just as the blog post suggests, I've put paperclip into the config/initializers directory,
I installed the gem, the gem is in the gemfile (rails 3 right), I ran the bundler.

In my user class, I've added

require 'paperclip'

When I load the app, I get the following error,

undefined method 'has_attached_file' for User:Class

The paperclip file looks like this

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

Any suggestions on what I may be doing wrong? have I got the steps right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幻梦 2024-09-18 20:46:41

从 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 初始值设定项。

As of MongoMapper 0.11.0, Paperclip 2.5.2, and Rails 3.0.4, all you need is:

#your model
require 'paperclip'

class User
  include MongoMapper::Document
  include Paperclip::Glue

  has_attached_file :avatar

  key :avatar_file_name, String
end

You don't need a Paperclip initializer anymore.

夏花。依旧 2024-09-18 20:46:41

在上面的代码片段对于更高版本的回形针和轨道不太适用之后,我刚刚发布了一个 gem 来解决这个问题。

查看 mongomapper-paperclip

I just released a gem to take care of this after the above code snippets didn't quite work for later versions of paperclip and rails.

Check out mongomapper-paperclip

嘿咻 2024-09-18 20:46:41

事实证明我

 
    include Paperclip
    require 'paperclip'

在 user.rb 文件中需要两者。

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

# unless attachement.valid?

现在情况正在好转。

turns out I needed both

 
    include Paperclip
    require 'paperclip'

in the user.rb file.

This lead to an error where vald? was not recognized, but I commented out

# unless attachement.valid?

and now things are going better.

ゃ人海孤独症 2024-09-18 20:46:41

我想我已经成功了,但一些原始的解决方案不适用于新发布的版本。此解决方案适用于 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

I think I got it working but some of the original solutions weren't working with the newly released versions. This solution works for Rails 3.0.4, paperclip 2.3.8, and mongo_mapper 0.8.6:

model:

# 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

initializer

# 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文