Ruby on rails Rails 4 Carrier在更新时有重复的条目

Ruby on rails Rails 4 Carrier在更新时有重复的条目,ruby-on-rails,duplicates,carrierwave,Ruby On Rails,Duplicates,Carrierwave,我正在使用Carrierwave gem将附件模块添加到我的应用程序中。为此,我创建了一个名为Attachable的模型关注点,如下所示: module Attachable extend ActiveSupport::Concern included do has_many :attachments, as: :attachable, dependent: :destroy accepts_nested_attributes_for :attachments en

我正在使用Carrierwave gem将附件模块添加到我的应用程序中。为此,我创建了一个名为
Attachable
的模型关注点,如下所示:

module Attachable
  extend ActiveSupport::Concern

  included do
    has_many :attachments, as: :attachable, dependent: :destroy
    accepts_nested_attributes_for :attachments
  end
end
我把它称为我的帖子模型:

class Post < ActiveRecord::Base
  include Attachable
end
当我更新时,它实际上复制了我的条目(数据库):

def update
  @post.clean_taxonomies
  if @post.update( post_params )
    redirect_to post_path(@post.slug)
  else
    @categories = Term.get_category
    render :edit
  end
end
编辑时,它会重新填充所有附件标题(字段名:我的表格中的字符串),但由于它无法将文件名重新填充到
输入字段中
,因此在保存修改时,它会使用空文件复制附件数

如何修复该问题并仅在已定义附件的情况下上载/保存附件

谢谢

编辑:
这是我在
posts/edit.html.haml
上的查看源代码(与new.html.haml相同):


好的,我终于找到了解决办法。如果我的
接受
的嵌套属性,我需要使用
拒绝作为:

accepts_nested_attributes_for :attachments,
                              allow_destroy: true,
                              reject_if: lambda { |a| a['file'].blank? }

它就像一个符咒:)

你能发表你的观点吗?问题很可能在那里。嗨,谢谢你的回答。我编辑了我的帖子并添加了查看代码。谢谢
= form_for @product, url: product_path(@product.slug), html: { multipart: true } do |f|
  = f.fields_for :attachments do |attachment|
    .row.attachment
      .col-sm-6
        = attachment.label :name, 'Filename'
        = attachment.text_field :name, class: 'form-control'
      .col-sm-5.col-xs-9
        = attachment.label :file, 'File'
        = attachment.file_field :file, class: 'form-control'
      .col-sm-1.col-xs-1
        %a.remove-attachment{ href: '#' }
          %i.icon-remove-circle.icon-2x
accepts_nested_attributes_for :attachments,
                              allow_destroy: true,
                              reject_if: lambda { |a| a['file'].blank? }