我想在我的应用程序用户能够上传文件与回形针尺寸验证错误4
至少
width:800px
和height: 550px
我创建的应用程序/模型/ dimensions_validator.rb文件 和代码
class DimensionsValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
dimensions = Paperclip::Geometry.from_file(value.queued_for_write[:original].path)
width = options[:width]
height = options[:height]
record.errors[attribute] << "Width must be at least #{width}px" if dimensions.width < width
record.errors[attribute] << "Height must be at least #{height}px" if dimensions.height < height
end
end
在我应用程序/模型/ gig.rb模型
validates :image, :dimensions => { :width => 800, :height => 550 }
问:当我点击提交按钮,实际上并没有选择任意图片,它得来的错误说
undefined method "path" for nil:NilClass
和它在红色标志着4号线是dimensions = Paperclip::Geometry.from_file(value.queued_for_write[:original].path)
也许我需要一个代码,以检查图像存在,像if image.present?
但瓦特我会在这里包括吗?我在演出模式validates_attachment_presence :image
这已经使用是错误
这是我GigsController#更新
def update
if @gig.update(gig_params)
redirect_to @gig, notice: "Gig was successfully updated"
else
render "edit"
end
end
对不起,你的意思是在第一个o新来的?你能否写下它作为答案。谢谢。 –
你能检查你的控制器是否允许params包含'image'?您还可以在日志输出中查找类似'Unpermitted parameter:image'的内容。 – Curtis
是的,它是允许的。并且在日志中,我得到了 NoMethodError(未定义的方法路径为零:NilClass): app/models/dimensions_validator.rb:3:在'validate_each' app/controllers/gigs_controller。rb:71:'update' –