4

モデル商品と商品画像があります。Product_imagesはペーパークリップモデルです。製品には多くのproduct_imagesがあります。複数の画像をアップロードし、これらの画像を製品ビューページに表示するアクティブ管理フォームを作成しています。

しかし、私が製品を保存するとき。productテーブルは更新されますが、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

そしてそれは常に「失敗」を引き起こします

4

2 に答える 2

2

レール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
于 2014-04-27T16:57:01.460 に答える
1

関連付けを宣言するときにオプションActiveRecordを追加することにより、モデルのコレクション内のアイテムへの変更をカスケード保存するように構成できます。:autosave => true

試す:

 has_many :product_images, :dependent => :destroy, :autosave => true

after_saveまた、次のように、コールバックで関連付けに関するテーブルデータを保存できます。

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
于 2013-01-23T14:09:23.330 に答える