Ruby on rails 无法更新活动管理嵌套表单中的回形针图像

Ruby on rails 无法更新活动管理嵌套表单中的回形针图像,ruby-on-rails,paperclip,activeadmin,Ruby On Rails,Paperclip,Activeadmin,我有产品模型和产品图片。Product_images是一款回形针型号。产品有很多产品图片。我正在制作一个活动的管理表单,该表单上载多个图像并将这些图像显示到产品视图页面 但是,当我保存产品时。更新产品表,但不更新产品图像表。图像附加正常,但我无法更新已上载图像的字段 class Product < ActiveRecord::Base attr_accessible :name, :product_images_attributes has_many :product

我有产品模型和产品图片。Product_images是一款回形针型号。产品有很多产品图片。我正在制作一个活动的管理表单,该表单上载多个图像并将这些图像显示到产品视图页面

但是,当我保存产品时。更新产品表,但不更新产品图像表。图像附加正常,但我无法更新已上载图像的字段

class Product < ActiveRecord::Base
    attr_accessible :name, :product_images_attributes 
      has_many :product_images, :dependent => :destroy
      accepts_nested_attributes_for :product_images, :reject_if => lambda { |t| t['product_image'].nil? }, :allow_destroy => true
end

class ProductImage < ActiveRecord::Base
    attr_accessible :name, :style
    attr_accessible :image 
    belongs_to :product
    has_attached_file :image, :styles => { :small => "150x150>", :large => "320x240>" }
    validates_attachment_presence :image
end

ActiveAdmin.register Product do
form :html => { :multipart => true } do |f|
    f.inputs "Admin Details" do
      f.input :name
    end
    f.inputs "Product images" do        
        f.has_many :product_images do |p|
            p.input :style, :as => :select, :collection => Image::STYLES, :include_blank => false
            p.input :image, :as => :file, :label => "Image",:hint => p.object.image.nil? ? p.template.content_tag(:span, "No Image Yet") : p.template.image_tag(p.object.image.url(:small))
            p.input :_destroy, :as=>:boolean, :required => false, :label => 'Remove image'
        end 
    end
    f.buttons
end

而且总是会引发“fail”

您可以配置
ActiveRecord
,通过在声明关联时添加
:autosave=>true
选项来级联保存对模型集合中项目的更改

尝试:

此外,您还可以在保存后的
回调中保存关联表数据,如下所示:

class Product < ActiveRecord::Base
    attr_accessible :name, :product_images_attributes 
      has_many :product_images, :dependent => :destroy
      accepts_nested_attributes_for :product_images, :reject_if => lambda { |t| t['product_image'].nil? }, :allow_destroy => true

       after_save :do_product_image_update

private
    def do_product_image_update
       self.product_images.save
    end

end
类产品:销毁
接受:product_images的_嵌套_属性,:reject_if=>lambda{t|t['product_images'].nil?},:allow_destroy=>true
保存后:是否更新产品图像
私有的
def do_产品_图像_更新
self.product\u images.save
结束
结束

我在使用rails 4.1时遇到了相同的错误。刚刚解决了这个问题。 我注意到输出中的警告:

Unpermitted parameters: _destroy, id
Unpermitted parameters: _destroy, id
Unpermitted parameters: _destroy, id
所以我只是通过它们来允许参数:

permit\u params assets\u属性:[:image,:image\u file\u name,:image\u content\u type,:image\u file\u size,:image\u updated\u at,:\u destroy,:id]

转到AA的资源页。它成功了。 我希望这会有帮助。 如果没有-这是我的清单

ActiveAdmin.register Product do

  permit_params :name, :description, :price, :brand_id, :category_id, assets_attributes: [:image, :image_file_name, :image_content_type, :image_file_size, :image_updated_at, :_destroy, :id]

  form multipart: true do |f|

      f.inputs "Детали" do
        f.input :category_id, as: :select, collection: Category.all, include_blank: false
        f.input :brand_id, as: :select, collection: Brand.all, include_blank: false

        f.input :name
        f.input :description
        f.input :price
      end

      f.inputs 'Фотографии' do
        f.has_many :assets, allow_destroy: true, heading: 'Фото', new_record: false do |fasset|
          fasset.input :image, as: :file, hint: fasset.template.image_tag(fasset.object.image.url(:thumb))
        end
      end
      f.actions
  end

end


class Product < ActiveRecord::Base
  belongs_to :category
  belongs_to :brand
  has_many :assets, dependent: :destroy, autosave: true
  accepts_nested_attributes_for :assets, allow_destroy: true,
                                :reject_if => lambda { |attributes| attributes[:image].blank? }

  validate :name, presence: true
  validate :description, presence: true
  validate :price, presence: true

end


class Asset < ActiveRecord::Base
  belongs_to :product
  has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"

  validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
  validates_with AttachmentPresenceValidator, :attributes => :image

end
ActiveAdmin.register产品do
许可参数:名称、描述、价格、品牌id、类别id、资产属性:[图像、图像文件名、图像内容类型、图像文件大小、图像更新处、销毁处、id]
表单多部分:真do | f|
f、 输入“ааааааа”do
f、 输入:category\u id,as::select,collection:category.all,include\u blank:false
f、 输入:brand\u id,as::select,collection:brand.all,include\u blank:false
f、 输入:姓名
f、 输入:说明
f、 输入:价格
结束
f、 输入“Фааааааааа”do
f、 有很多:资产,允许销毁:真,标题:'Фццццц',新记录:假do | fasset|
fasset.input:image,as::file,提示:fasset.template.image_标记(fasset.object.image.url(:thumb))
结束
结束
f、 行动
结束
结束
类产品lambda{| attributes | attributes[:image]。blank?}
验证:名称、状态:true
验证:描述,存在:真
验证:价格,存在:真实
结束
类别资产{:medium=>“300x300>”,:thumb=>“100x100>”},:default\u url=>“/images/:style/missing.png”
验证\u附件\u内容\u类型:图像,:内容\u类型=>/\Aimage\/.\Z/
使用AttachmentPresenceValidator验证_,:attributes=>:image
结束

我可以更改附件图像,但我不能更改附件的样式。不,即使我在更新后调用
并在我的ProductImage模型中引发一些异常,它也不会引发。在
ProductImage
表中是否有
product\u id
字段?如果是这样,您能在更新后的
回调中给出异常详细信息吗?是的,我有这个外键。在这个回调中,我只是简单地提出了“图像更新!!!”
,但它从未发生过。所以你自己告诉我实际的问题在哪里。由于样式字段从未更新,因此如果ProductImage.find\u by\u product\u id(self.id).changed?
条件变为false。因此,您将获得
提升失败
Unpermitted parameters: _destroy, id
Unpermitted parameters: _destroy, id
Unpermitted parameters: _destroy, id
ActiveAdmin.register Product do

  permit_params :name, :description, :price, :brand_id, :category_id, assets_attributes: [:image, :image_file_name, :image_content_type, :image_file_size, :image_updated_at, :_destroy, :id]

  form multipart: true do |f|

      f.inputs "Детали" do
        f.input :category_id, as: :select, collection: Category.all, include_blank: false
        f.input :brand_id, as: :select, collection: Brand.all, include_blank: false

        f.input :name
        f.input :description
        f.input :price
      end

      f.inputs 'Фотографии' do
        f.has_many :assets, allow_destroy: true, heading: 'Фото', new_record: false do |fasset|
          fasset.input :image, as: :file, hint: fasset.template.image_tag(fasset.object.image.url(:thumb))
        end
      end
      f.actions
  end

end


class Product < ActiveRecord::Base
  belongs_to :category
  belongs_to :brand
  has_many :assets, dependent: :destroy, autosave: true
  accepts_nested_attributes_for :assets, allow_destroy: true,
                                :reject_if => lambda { |attributes| attributes[:image].blank? }

  validate :name, presence: true
  validate :description, presence: true
  validate :price, presence: true

end


class Asset < ActiveRecord::Base
  belongs_to :product
  has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"

  validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
  validates_with AttachmentPresenceValidator, :attributes => :image

end