回形针:删除附件和“无法将nil转换为字符串”错误

时间:2010-04-26 11:32:48

标签: ruby-on-rails paperclip

我正在使用Paperclip,这是我在模型中删除附件的行为:

  def before_save
    self.avatar = nil if @delete_avatar == 1.to_s 
  end

除非在用户实际上传图片时设置了@delete_avatar标记,否则工作正常(因此模型会同时收到params[:user][:avatar]params[:user][:delete_avatar]。这会导致以下错误:

TypeError: can't convert nil into String
    from /Work/project/src/vendor/plugins/paperclip/lib/paperclip/storage.rb:40:in `dirname'
    from /Work/project/src/vendor/plugins/paperclip/lib/paperclip/storage.rb:40:in `flush_writes'
    from /Work/project/src/vendor/plugins/paperclip/lib/paperclip/storage.rb:38:in `each'
    from /Work/project/src/vendor/plugins/paperclip/lib/paperclip/storage.rb:38:in `flush_writes'
    from /Work/project/src/vendor/plugins/paperclip/lib/paperclip/attachment.rb:144:in `save'
    from /Work/project/src/vendor/plugins/paperclip/lib/paperclip/attachment.rb:162:in `destroy'
    from /Work/project/src/app/models/user.rb:72:in `before_save'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/callbacks.rb:347:in `send'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/callbacks.rb:347:in `callback'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/callbacks.rb:249:in `create_or_update'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2538:in `save_without_validation'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/validations.rb:1078:in `save_without_dirty'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/dirty.rb:79:in `save_without_transactions'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/transactions.rb:229:in `send'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/transactions.rb:229:in `with_transaction_returning_status'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/transactions.rb:182:in `transaction'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/transactions.rb:228:in `with_transaction_returning_status'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/transactions.rb:196:in `save'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/transactions.rb:208:in `rollback_active_record_state!'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/transactions.rb:196:in `save'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:723:in `create'

我认为它与avatar.dirty?值有关,因为当这种情况发生时肯定是真的。问题是,如果要保存更改并在设置标志时中止头像上传,如何完全重置事物?

1 个答案:

答案 0 :(得分:5)

Paperclip将上传的文件样式排队到散列中:

{:original=>#<File:/tmp/stream20100427-6708-17hkcjs-0,6708,0>,  .... }

删除文件时不会清除此哈希值。因此需要手动完成:

 def before_save
    if @delete_avatar == 1.to_s 
      self.avatar.queued_for_write.clear
      self.avatar = nil 
    end
  end