Clip with MongoMapper in Rails 3

I am trying to implement Paperclip in my first rails application, and I happen to use rails 3 and mongodb with mongomapper.

I followed this guide on how everything works together

According to the blog, I put the paperclip in the config / initializers directory, I installed the gem, the gem is in the gemfile (rails 3 on the right), I started the picker.

In my user class, I added

require 'paperclip'

When I download the application, I get the following error:

undefined method 'has_attached_file' for User: Class

The clip file is as follows:

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 might be doing wrong? Do I have the right steps?

+3
source share
4 answers

Starting with 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 no longer need a Paperclip initializer.

+6
source

, , paperclip rails.

mongomapper-paperclip

+1

i need both

 
    include paperclip
    require 'paperclip'

in user.rb.

This will result in an error when vald? was not recognized but I commented

# unless attachement.valid?

and now everything is going better.

0
source

I think I worked, but some of the original solutions did not work with recently 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
0
source

Source: https://habr.com/ru/post/1756826/


All Articles