Ruby on rails 验证消息中的回形针文件名

Ruby on rails 验证消息中的回形针文件名,ruby-on-rails,validation,file,paperclip,Ruby On Rails,Validation,File,Paperclip,以下是我在模型中尝试做的: 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

以下是我在模型中尝试做的:

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)
我找不到解决方案如何在
原始文件名中获取文件名

NameError (undefined local variable or method `photo_file_name' for #<Class:0xaafb004>):
namererror(未定义的局部变量或#的'photo_file_name'方法):

NoMethodError(未定义的方法“photo”表示#):

尝试
照片文件名
而不是
照片。原始文件名

有关这方面的更多信息,请参阅

希望能有所帮助。

self.photo.url

问题在于self不是实例,而是类

您可以按如下方式使用上载的内容类型:

validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'],
                            :message => :inclusion
然后在翻译文件中,添加

activerecord.errors.models..attributes.photo.inclusion:“%{value}不允许”

其中值将替换为上载的内容类型

请尝试使用

self.photo.instance_read(:file_name)
有关
回形针::附件#实例_read
的更多信息,请参阅文档


希望这能有所帮助。

你能在这里发布你犯了什么错误吗?如果这么简单,我就不会问这个问题了。很抱歉,没用。谢谢你的回答,但是我说的是文件名,而不是内容类型。你可以考虑修改P夹,把原始文件名传递给消息内插。
self.photo.instance_read(:file_name)