3

私は最初の Rails アプリに Paperclip を実装しようとしていますが、たまたま Rails 3 と mongodb を mongomapper で使用しています。

私はこのガイドに従って、すべてがうまく機能するようにしました

ブログ投稿が示唆するように、ペーパークリップを config/initializers ディレクトリに配置し、gem をインストールし、gem ファイル (Rails 3 の右) にあり、bundler を実行しました。

私のユーザークラスでは、追加しました

「ペーパークリップ」が必要

アプリをロードすると、次のエラーが表示されます。

User:Class の未定義のメソッド 'has_attached_file'

クリップファイルはこんな感じ

モジュール ペーパークリップ
  モジュール ClassMethods
    def has_attached_file 名、オプション = {}
      InstanceMethods を含める

      write_inheritable_attribute(:attachment_definitions, {}) if attachment_definitions.nil?
      attachment_definitions[name] = {:validations => []}.merge(オプション)

      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(名前)
        (args.length > 0) ? a.to_s(args.first) : a
      終わり

      define_method "#{name}=" do |ファイル|
        attachment_for(name).assign(ファイル)
      終わり

      define_method "#{name}?" 行う
        attachment_for(name).file?
      終わり

      validates_each name, :logic => lambda {
        attachment = attachment_for(name)
        attachment.send(:flush_errors) attachment.valid?
      }
    終わり
  終わり

  モジュール補間
    # 文字列 ID の処理 (mongo)
    def id_partition アタッチメント、スタイル
      if (id = attachment.instance.id).is_a?(整数)
        ("%09d" % id).scan(/\d{3}/).join("/")
      そうしないと
        id.scan(/.{3}/).first(3).join("/")
      終わり
    終わり
  終わり
終わり

私が間違っているかもしれないことについて何か提案はありますか? 手順は正しいですか?

4

4 に答える 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 イニシャライザはもう必要ありません。

于 2012-02-06T07:52:30.420 に答える
1

上記のコードスニペットが後のバージョンのペーパークリップとレールでうまく機能しなかったため、これを処理するための宝石をリリースしました。

mongomapper-paperclip をチェックしてください

于 2012-08-16T18:52:13.813 に答える
0

両方が必要だったことがわかりました

 
    ペーパークリップを含む
    「ペーパークリップ」が必要

user.rbファイル内。

これは、valdのエラーにつながりますか?認識されませんでしたが、コメントアウトしました

#attachement.validでない限り?

そして今、物事は良くなっています。

于 2010-07-29T17:55:43.820 に答える
0

私はそれを機能させたと思いますが、元のソリューションのいくつかは新しくリリースされたバージョンでは機能していませんでした。このソリューションは、Rails 3.0.4、paperclip 2.3.8、およびmongo_mapper0.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
于 2011-03-16T20:41:39.230 に答える