2013-02-22 66 views
2

我遇到了一個問題,在試圖上傳無效文件類型時,我的模型附件字段上的驗證錯誤未顯示在視圖中。我的模型的設置如下:在simple_form中顯示回形針content_type錯誤

has_attached_file :avatar, 
    styles: {medium: "300x300>", thumb: "100x100>"} 

validates_attachment :avatar, presence: true, 
    content_type: { content_type: /^image\/(jpg|jpeg|pjpeg|png|x-png|gif)$/ }, 
    size: { in: 0..10.megabytes } 

如果我檢查我調試錯誤,我看到以下內容:

(rdb:1) p @applicant.errors.to_hash 
{:avatar_content_type=>["is invalid"]} 

我所知道的驗證工作,所以我懷疑的問題是,錯誤出現在「avatar_content_type」字段,而不是「avatar」字段。我發現了一個建議和similar issue on the simple_form discussion forum一個是使用錯誤幫手:

<%= f.input :avatar %> 
<%= f.error :avatar_content_type %> 

這類作品,但不包括標記和類,正常的錯誤包括,不正確的風格。有沒有一種方法可以讓我忽略這種方法,它可以用於twitter引導程序?

回答

2

一位超級黑客,把這個在你看來上述表單:

<% @applicant.errors[:avatar] << "Invalid avatar upload format" if @applicant.errors[:avatar_content_type] %> 

編輯:更好的答案:

做相同的控制器

def update 
    @applicant = Applicant.find(params[:id]) 
    if @applicant.update_attributes(params[:applicant]) 
    # .. 
    else 
    @applicant.errors[:avatar] << "Invalid avatar upload format" if @applicant.errors[:avatar_content_type] 
    render :edit 
    end 
end