Ruby on Rails PaperClip Gem validate_attachement error



我在谷歌上搜索/堆栈溢出了几个小时,但没有找到这个问题的解决方案。我想知道我的PaperClip安装是否不成功。我正在尝试验证我的模型文件夹中的图像附件:

validates :image, presence: true,
content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif']},
size: { less_than: 5.megabytes }

我还尝试了与github:上的read-me文件更相似的代码

validates_attachment :image, :presence => true,
:content_type => { :content_type => 'image/jpeg', 'image/jpg', 'image/png', 'image/gif' },
:size => { less_than: => 5.megabytes }

我试着使用个人验证

validates_attachment_presence :image
validates_attachment_content_type :image,:content_type => ['image/jpeg', 'image/jpg', 'image/png', 'image/gif']
validates_attachment_size :image,:less_than => 5.megabytes

我在任何情况下都会出错。任一:

Routing Error
undefined method `before_image_post_process' for #<Class:0x00000101461750>
Try running rake routes for more information on available routes.

或者:

NoMethodError in PinsController#index
undefined method `key?' for nil:NilClass

您的文件中有has_attached_file :image吗?如果是,请确保它在validates_attachment之前。

我每次都会收到这个错误,只是因为我总是忘记以相同的方式重命名图像变量(从片段复制后):

has_attached_file :avatar...
validates_attachment_content_type :photo, :content_type...

→也应该是:avatar而不是:photo

这是一个很好的例子,说明了当代码不是DRY时,问题可能会被剽窃。

savmac的修复程序刚才对我有效。我在heroku公开赛上也遇到了同样的问题。模型中的行以前已经坏了,我的应用程序已经无缝工作了几个月——不知道发生了什么变化。谢谢

最新更新