2010-07-27 62 views
3

我試圖在我的第一個rails應用程序中實現Paperclip,而我碰巧在mongomapper中使用rails 3和mongodb。回形針與MongoMapper在Rails 3中

我在到達那裏的東西大家共同努力

正如博客文章建議,我已經把回形針到配置/初始化目錄, 我安裝了寶石跟着this guide,創業板在的Gemfile (導軌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 

關於我可能做錯什麼的建議?我有正確的步驟嗎?

回答

0

原來我在user.rb文件同時需要

 
    include Paperclip 
    require 'paperclip' 

這會導致vald錯誤嗎?沒有得到承認,但我注意到

# unless attachement.valid?

現在事情進展得更好。

0

我覺得我的工作原理,但一些原始的解決方案沒有與新發布的版本。該解決方案適用於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 
6

由於MongoMapper 0.11.0,回形針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 

你不需要一個回形針初始化器了。

1

上面的代碼片段對回形針和導軌的後續版本不起作用後,我剛剛發佈了一個gem來處理此問題。

結賬mongomapper-paperclip