错误:验证失败:必须存在可成像的图像,rails-5.0,paperclip-5 [英] Error: Validation failed: Images imageable must exist , rails-5.0 , paperclip-5
问题描述
当我尝试提交表单时,发生以下错误:Validation failed: Images imageable must exist
并呈现相同的new.html.erb
视图.
While i was trying to submit the form, following error occured: Validation failed: Images imageable must exist
and render the same new.html.erb
view.
如果我评论new.html.erb
中的file field
.产品创建成功.
If i comment the file field
in new.html.erb
. Product is being created successfully.
ProductsController:
def new
@product = Product.new
end
def create
@product = Product.create!(product_params)
if @product.save
redirect_to products_path, notice: "Product Created Successfully"
else
render "new"
end
end
def product_params
params.require(:product).permit(:name, :quantity, :price, images_attributes: [:id, :photo, :_destroy])
end
new.html.erb:
<%= nested_form_for @product, html: { multipart: true } do |f|%>
<h2>New</h2>
<P> <%= f.label :name %> <%= f.text_field :name %> </P>
<P> <%= f.label :quantity %> <%= f.text_field :quantity %> </P>
<P> <%= f.label :price %> <%= f.text_field :price %> </P>
<%= f.fields_for :images do |p| %>
<p> <%= p.label :photo %> <%= p.file_field :photo %> </p>
<%= p.link_to_remove "Remove Image" %>
<% end %>
<%= f.link_to_add "Add Image", :images %>
<%= f.submit "Add Product" %>
<% end %>
20160725102038_add_image_columns_to_imageable.rb:
class AddImageColumnsToImageable < ActiveRecord::Migration[5.0]
def up
add_attachment :images, :photo
end
def down
remove_attachment :images, :photo
end
end
型号:product.rb
class Product < ApplicationRecord
has_one :variant
has_many :images, as: :imageable, dependent: :destroy
accepts_nested_attributes_for :images, allow_destroy: true
end
型号:image.rb
class Image < ApplicationRecord
belongs_to :imageable, polymorphic: 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
推荐答案
在rails 5中,belongs_to
确保关联的模型必须存在.
例如,在此多态关联中,图像模型具有belongs_to :imageable
,而产品模型具有has_many :images
.
因此,在new.html.erb
中,我们正在创建图像,但是相应的产品不存在,所以这就是错误Image imageable must exist
的原因.
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
.
解决方案
在图像模型中关联belong_to
时添加optional: true
.
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
这篇关于错误:验证失败:必须存在可成像的图像,rails-5.0,paperclip-5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!