gpt4 book ai didi

ruby-on-rails - 无法在事件管理嵌套表单中更新回形针图像

转载 作者:行者123 更新时间:2023-12-01 02:28:43 25 4
gpt4 key购买 nike

我有模型产品和产品图片。 Product_images 是一个回形针模型。产品有很多 product_images。我正在制作一个 Active Admin 表单,该表单上传多个图像并将这些图像显示到产品 View 页面。

但是,当我保存产品时。产品表已更新,但 product_image 表未更新。图片正常附加,但我无法更新已上传图片的字段。

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

更新

在产品模型中,我这样做:
  after_update :check

def check
if ProductImage.find_by_product_id(self.id).changed?
raise "image"
else
raise "fail"
end
end

它总是引发“失败”

最佳答案

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

Unpermitted parameters: _destroy, id
Unpermitted parameters: _destroy, id
Unpermitted parameters: _destroy, id

所以我只是将它们传递给 permit_params:
permit_params assets_attributes: [:image, :image_file_name, :image_content_type, :image_file_size, :image_updated_at, :_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

关于ruby-on-rails - 无法在事件管理嵌套表单中更新回形针图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14481458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com