Ruby on rails 回形针:删除附件和";can';t将nil转换为字符串";错误

Ruby on rails 回形针:删除附件和";can';t将nil转换为字符串";错误,ruby-on-rails,paperclip,Ruby On Rails,Paperclip,我使用的是回形针,以下是我在模型中删除附件的操作: def before_save self.avatar = nil if @delete_avatar == 1.to_s end 除非在用户实际上载图像时设置了@delete\u avatar标志(因此模型同时接收参数[:user][:avatar]和参数[:user][:delete\u avatar],否则工作正常。这将导致以下错误: TypeError: can't convert nil into String

我使用的是回形针,以下是我在模型中删除附件的操作:

  def before_save
    self.avatar = nil if @delete_avatar == 1.to_s 
  end
除非在用户实际上载图像时设置了
@delete\u avatar
标志(因此模型同时接收
参数[:user][:avatar]
参数[:user][:delete\u 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'

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

回形针将上载的文件样式以散列形式排列:

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

谢谢,解决方案是正确的,除了我必须切换self.avatar=nil和self.avatar.queued_以获得_write.clear行。否则同样的错误也会发生。嗯,我只在手动分配的文件上测试了它,而不是从表单上传。可能这就是问题所在。答案中的行被切换了,所以首先是排队等待_write.clear。
 def before_save
    if @delete_avatar == 1.to_s 
      self.avatar.queued_for_write.clear
      self.avatar = nil 
    end
  end