错误:验证失败:可成像的图像必须存在,rails-5.0,paperclip-5

2023-12-31

当我尝试提交表单时,发生以下错误:Validation failed: Images imageable must exist并渲染相同的new.html.erb view.

如果我评论file field in new.html.erb。产品正在成功创建。

产品控制器:


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

新的.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

在轨道 5 中,belongs_to确保关联的模型必须存在。 例如,在这种多态关联中,图像模型具有belongs_to :imageable并且产品型号有has_many :images。 所以在这里new.html.erb我们正在创建图像,但相应的产品不存在,所以这就是错误的原因Image imageable must exist .

Solution


Add optional: true在建立关联时belong_to在图像模型中。

图像模型现在看起来像:

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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

错误:验证失败:可成像的图像必须存在,rails-5.0,paperclip-5 的相关文章

随机推荐

  • 静态成员函数的局部变量

    今天我们遇到了一个关于多线程环境中静态成员函数的问题 我们问自己但找不到满意答案的问题是 静态成员函数的局部变量也是静态的吗 header class A static int test implementation int A test
  • pandas dataframe 删除每个数字的第一行

    所以 基本上我有一个数据框 其第一列如下所示 1 2 2 3 3 3 3 4 4 5 如您所见 第一列由随机重复的数字组成 首先 我必须删除所有 然后删除每个数字的第一行 由于 1 只有一行 它应该消失 并且只有 2 的第二行保留 依此类推
  • 什么是 ASP.NET 提供程序?

    ASP NET Provider 中的 Provider 是什么意思 例如当我们谈论AuthorizationRuleProvider connectionStrings providerName 这是一种设计模式 提供商模型 http e
  • 在 foreach 中的 DataRow 上转换 null 文字或可能的 null 警告

    更新了我的 NET Core 3 1 控制台应用程序以使用 Nullables 功能并具有以下内容foreach loop DataTable profiles DB ListProfiles CS8600 warning here v f
  • AttributeError:模块“pandas”没有属性“to_csv”

    我从 csv 文件中取出了一些行 如下所示 pd DataFrame CV data take 5 columns CV data columns 并对其执行一些功能 现在我想再次将其保存在 csv 中 但出现错误module pandas
  • 如何更改 shell/bash 中的列名称?

    这个问题对于大多数人来说可能太简单了 但我真的不想手动做 所以 假设我在终端中有一个文件 如下所示 dz2t edison s GWAS head PHENO GWASID CHILDID VSTNUM GENDER GA 1001 100
  • 使用 ng-repeat 生成的项目中的增量计数器

    我用 ng repeat 生成了一个列表 其中每个项目都有一个计数变量 在列表项中 我有一个链接 我想在单击它时增加计数 我不知道如何用 Angular JS 方式解决这个问题 Plunker http plnkr co edit HfeL
  • Android googlemaps v2完成加载事件或回调

    我想在谷歌地图加载后做一些事情 maptiles已被填充 无论如何可以实现这一点吗 正如所指出的qubz https stackoverflow com users 496625 qubz ViewTreeObserver可用于实现地图加载
  • Angular2 检测模板视图中的元素是否具有类

    我们使用 bootstrap 有时它会自动向 DOM 元素添加类 附加到这些元素并检测何时将特定 css 类添加到组件模板子元素的最佳方法是什么 假设我有这个组件 import Component ViewChild ElementRef
  • 防止默认事件操作不起作用...?

    I m trying to add keyboard shortcuts on my website to make fast navigation possible using the keyboard I m running into
  • JPA本机查询返回类

    在 JPA 中 我定义了一个本机 sql 它将返回 String NamedNativeQuery name alert query select distinct c accountId from account c resultClas
  • Flutter Firestore 服务器端时间戳

    我需要在使用 Flutter 应用程序添加到 Firestore 的新文档上添加服务器端时间戳 我明白我应该使用FieldValue serverTimestamp但我不知道在哪里可以找到这个 截至9月5日 更新cloud firestor
  • 如何在 Tkinter 中使用 Canvas 绘制点?

    我想在Tkinter中画一个点 现在我正在使用Canvas做到了 但我没有找到这样的方法来画点Canvas class Canvas提供了一个方法叫做crete line x1 y1 x2 y2 所以我尝试设置x1 x2 y1 y2画一个点
  • 自动检查父复选框

    我有一个 AJAX 请求 该请求会拉入一个文件夹列表 每个列表项旁边都有一个复选框 如果我选中父文件夹旁边的复选框 则会通过执行以下操作自动检查所有子文件夹 var checkChildCheckBoxes function var isA
  • 容器启动后在 Docker 上运行 mongorestore

    我正在尝试设置一个运行 MongoDB 的容器 该容器在启动时使用 mongorestore 填充数据 这个想法是快速建立一个用于测试和模拟的虚拟数据库 我的 Dockerfile 如下所示 FROM mongo bionic COPY d
  • 在 Eclipse Neon 中启用 JSDT 大纲视图

    显示 JavaScript 源文件内容的大纲视图问题并不新鲜 而且它甚至找到了答案 https stackoverflow com a 11062205 2749913 然而 这个答案在 Neon 4 6 0 中不再有效 我打开了在两个单独
  • Delphi增量变化?从 D7 到 D2009

    是否有网站或文档详细说明每个版本所做的更改 比如说 D7 gt D2005 gt D2006 gt D2007 gt D2009 语言特点 数据库功能 RTTI IDE 必须查看 Google 缓存才能找到其中的几个 Delphi 2005
  • Nvidia 驱动程序更新后无法在 Qt 5.3 上编译 GLSL 着色器

    我有一些非常基本的 openGL 着色器 用于编译 但在 Nvidia 驱动程序更新后不再编译 每个着色器都以以下行开头 版本150核心 我曾经使用 QGLShaderProgram addShaderFromSourceFile 方法编译
  • 简单的 C++ 继承示例,有什么问题吗? [复制]

    这个问题在这里已经有答案了 可能的重复 派生类中具有相同名称但不同签名的函数 https stackoverflow com questions 411103 function with same name but different si
  • 错误:验证失败:可成像的图像必须存在,rails-5.0,paperclip-5

    当我尝试提交表单时 发生以下错误 Validation failed Images imageable must exist并渲染相同的new html erb view 如果我评论file field in new html erb 产品