[英]Paperclip file name in validation message
這是我在模型中嘗試做的事情:
has_attached_file :photo, :styles => self.image_sizes, :whiny => false
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'],
:message => I18n.t('paperclip.invalid_image_type', :file => self.photo.original_file_name)
我無法找到解決方案如何在original_file_name
獲取文件名:
NameError (undefined local variable or method `photo_file_name' for #<Class:0xaafb004>):
要么
NoMethodError (undefined method `photo' for #<Class:0xb303e7c>):
問題是自我不是實例,而是類 。
您可以使用上傳的內容類型,如下所示:
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'],
:message => :inclusion
然后在您的翻譯文件中添加
activerecord.errors.models.<modelname>.attributes.photo.inclusion: "%{value} is not allowed"
其中值將替換為上載的內容類型
嘗試使用
self.photo.instance_read(:file_name)
有關Paperclip::Attachment#instance_read
更多信息,請參閱此處的文檔。
希望這可以幫助。
嘗試使用photo_file_name
而不是photo.original_file_name
。
有關此內容的更多信息,請參閱方法:Paperclip :: Attachment#original_filename
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.