【问题标题】:Ruby on Rails PaperClip Gem validate_attachement errorRuby on Rails PaperClip Gem validate_attachement 错误
【发布时间】:2013-07-27 18:47:16
【问题描述】:

我已经用谷歌搜索/堆栈溢出了几个小时,但没有找到解决这个问题的方法。我想知道我的 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

【问题讨论】:

    标签: ruby-on-rails ruby validation paperclip attachment


    【解决方案1】:

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

    【讨论】:

    • 你说得对@savmac,顺序很重要。我有同样的问题,但我记得我在某处读到,关联应该总是在验证之前。
    • Paperclip 可能会抛出更好的错误,以防找不到传递给validates_attachment的附件!
    【解决方案2】:

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

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

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

    这是一个完美的例子,说明当代码不是 DRY 时可能会出现问题。

    【讨论】:

      【解决方案3】:

      savmac 的修复程序刚刚对我有用。我在打开heroku时遇到了同样的问题。模型中的线条之前出现故障,而我的应用程序已经无缝运行了几个月——不知道发生了什么变化。谢谢!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-03
        • 2017-06-15
        • 2010-11-23
        • 1970-01-01
        • 2013-01-13
        • 1970-01-01
        • 2015-07-15
        • 2019-03-01
        相关资源
        最近更新 更多