【问题标题】:rails 4 devise with paperclip, undefined method after_commitrails 4 用回形针设计,未定义的方法 after_commit
【发布时间】:2014-01-30 03:45:42
【问题描述】:

我在设备激活后遇到了奇怪的问题。带有回形针属性的模型 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 中,我玩过各种 gem,包括 strong_parameters 和 protected_attributes,但没有任何进展。

编辑: 我评论行 @klass.send(:after_commit, :on => :destroy) { send(name).send(:flush_deletes) } 和问题现在用 mongoid 正确实现回形针

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

【问题讨论】:

    标签: ruby-on-rails devise mongoid paperclip


    【解决方案1】:

    问题是回形针使用 after_commit 钩子进行交易,而 Mongoid 不支持它们。我的建议是你修补 Paperclip 以提供交易的解决方法。

    您也可以尝试mongoid-paperclip,我认为这正是您想要的。

    【讨论】:

      猜你喜欢
      • 2018-01-07
      • 1970-01-01
      • 1970-01-01
      • 2014-01-31
      • 2014-01-12
      • 1970-01-01
      • 2018-08-26
      • 2013-12-17
      • 1970-01-01
      相关资源
      最近更新 更多