0
得到錯誤這是我千兆模型當確認畫面出現軌道4
class Gig < ActiveRecord::Base
has_attached_file :image, :styles => { :medium => "360x170>", :bigger => "650x459>" }
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
validate :image_size_validation
def image_size_validation
if image.size > 2.megabytes
errors.add(:base, "Image should be less than 2MB")
end
end
end
一切都很正常,我不能上傳圖片大於2MB,因爲我想要的,它提供了有關通知(書面我)。
問:當我保存表單(新產品),而不選擇任何圖片,它拋出一個錯誤說
undefined method > for nil:NilClass
。而不是僅僅向用戶說,他應該上傳一張圖片,所以表格可以保存。
千兆控制器,用於創建本
def create
@gig = current_user.gigs.build(gig_params)
if @gig.save
redirect_to @gig, notice: "Gig successfully created"
else
render "new"
end
end
與方法的形式獲取保存沒有圖像,當它應該顯示一個通知,「直到你添加圖片,你就不可能前進」,給用戶。 –
應該通過在線驗證來捕捉丟失的圖像。 (如果需要,添加如下內容:'validates:image,presence:true')這只是確保圖像大小驗證在沒有圖像時不會爆炸 –