Error: Validation failed: Images imageable must exist , rails-5.0 , paperclip-5

ぐ巨炮叔叔 提交于 2019-12-03 06:19:59
Waqas Awan

In rails 5, belongs_to makes sure that the associated model must exist. E.g In this polymorphic association, Image model has belongs_to :imageable and Product model has has_many :images. So here in new.html.erb we are creating an image, but respective product not exist, so that's why error Image imageable must exist .

Solution


Add optional: true while making an association of belong_to in Image model.

Image Model now looks like:

class Image < ApplicationRecord
  belongs_to :imageable, polymorphic: true, optional: true

  has_attached_file :photo, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
  validates_attachment :photo, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }

end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!