加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 01:22:26 所属栏目:百科 来源:网络整理
导读:我正在尝试在我的第一个rails应用程序中实现Paperclip,我碰巧使用rails 3和 mongodb与mongomapper. 我跟着this guide一起去做所有工作的事情 正如博客文章所说,我已将paperclip放入config / initializers目录, 我安装了gem,gem在gemfile中(rails 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初始化程序.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读