2010-04-26 53 views
4

我使用回形針和這裏就是我在模型中刪除附件做:回形針:刪除附件和「不能轉換成零字符串」錯誤

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?價值,因爲當它是千真萬確w ^如果發生這種情況。問題是,如果有更改要保存,並且在設置標誌時中止頭像上傳,我該如何完全重置該事物?

回答

5

在哈希上傳文件的樣式回形針隊列:

{: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 
+0

謝謝,解決方案是正確的,除了我不得不切換self.avatar = nil和self.avatar.queued_for_write.clear行。否則會發生相同的錯誤。 – snitko 2010-04-27 21:21:40

+0

嗯,我只測試它在手動分配的文件,而不是從表單上傳。可能是這個問題。 答案中的切換線,所以queued_for_write.clear首先進行。 – Voyta 2010-04-27 21:42:15