Rails 4中使用Devise和Paperclip,出现undefined method after_commit错误

5

我在激活设备后遇到了奇怪的问题。带有Paperclip属性 has_mongoid_attached_file 的模型返回异常:

undefined method `after_commit' for Item:Class

/home/student/.rvm/gems/ruby-2.1.0/bundler/gems/paperclip-c9c5227cb7f8/lib/paperclip/has_attached_file.rb:89:in `add_active_record_callbacks'
/home/student/.rvm/gems/ruby-2.1.0/bundler/gems/paperclip-c9c5227cb7f8/lib/paperclip/has_attached_file.rb:19:in `define'
/home/student/.rvm/gems/ruby-2.1.0/bundler/gems/paperclip-c9c5227cb7f8/lib/paperclip/has_attached_file.rb:4:in `define_on'
/home/student/.rvm/gems/ruby-2.1.0/bundler/gems/paperclip-c9c5227cb7f8/lib/paperclip.rb:179:in `has_attached_file'
mongoid-paperclip (0.0.8) lib/mongoid_paperclip.rb:70:in `has_mongoid_attached_file'

据我理解,这个问题涉及到gems/paperclip-3.5.3/lib/paperclip/has_attached_file.rb中的after_commit的可见性。

def add_active_record_callbacks
      name = @name
      @klass.send(:after_save) { send(name).send(:save) }
      @klass.send(:before_destroy) { send(name).send(:queue_all_for_delete) }
      @klass.send(:after_commit, :on => :destroy) { send(name).send(:flush_deletes) }
end

在Gemfile中,我尝试了包括strong_parameters和protected_attributes在内的各种gem,但没有取得任何进展。

编辑: 我注释掉了这一行: @klass.send(:after_commit, :on => :destroy) { send(name).send(:flush_deletes) } 现在正确实现了mongoid中的paperclip

编辑: 在https://github.com/thoughtbot/paperclip/pull/1425/files中找到更好的解决方案。

1个回答

0
问题在于Paperclip使用事务的after_commit钩子,而Mongoid不支持它们。我的建议是您修补Paperclip以提供事务的解决方法。
或者,您可以尝试mongoid-paperclip,我认为这正是您要寻找的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接