2011-04-23 32 views
0

當我嘗試保存一個無效的模型對象時,我不記得一條錯誤消息,但我的應用程序卡住了,我期望測試失敗不會卡住。 (當使用有效的屬性,即指定一個圖像文件,一切都如預期的那樣,對象是有效的。)使用回形針時Rails3模型卡住has_attached_file

我發現它是因爲使用has_attached_file時的回形針:image 任何想法爲什麼會發生?

型號/ user_image.rb

has_attached_file :image 
:image_content_type, :presence => true 
validate :valid_res   

validates_attachment_content_type 
:image, :content_type => 
['image/jpeg','image/jpg','[image/jpeg]', 
'[image/png]', 'image/png'] ... 

user_image_spec.rb

describe UserImage do 

before(:each) do 
    @user_image = UserImage.new(
    :uid => "1234abca" 
) 
end 

it "is valid with valid attributes" do 
    @user_image.should be_valid 
end 

更新:我用回形針插件,所以我刪除它,並安裝了寶石。仍然得到相同的結果,我想安裝寶石後做些什麼?

回答

0

我不知道是什麼做這行的意思是:

:image_content_type, :presence => true 

我覺得這個會導致你的煩惱

+0

我刪除它仍然得到卡住。 – ddayan 2011-04-23 12:19:56

+1

什麼是'validate:valid_res'? – fl00r 2011-04-23 12:34:09

+0

是的,那是我創建的用於驗證圖像分辨率的驗證方法。我認爲如果沒有圖像,它會失敗,感謝您的幫助!現在我知道該怎麼解決。 – ddayan 2011-04-23 12:43:36

0

爲了驗證曲別針的圖像只需添加:

validates :image, :attachment_presence => true