繁体   English   中英

无法使用active_admin和carrierwave保存图像

[英]Can't save image with active_admin and carrierwave

我有下一个型号:

文章

class Article < ApplicationRecord
  validates :name, length: { maximum: 240 }, presence: true

  has_one :image,
          as: :imageable,
          class_name: 'CatalogImage',
          autosave: true,
          dependent: :destroy
  accepts_nested_attributes_for :image, allow_destroy: true
end    

CatalogImage

class CatalogImage < ApplicationRecord
  belongs_to :imageable, polymorphic: true

  validates :file, presence: true
  mount_uploader :file, ImageUploader
end

活动管理员表单:

form do |f|
  f.inputs do
    f.input :name
    f.input :content, as: :ckeditor
    f.input :position
    f.input :active, as: :radio
    f.input :place_on_home_page, as: :radio
    # f.input :image, as: :file

    # same with -> [:image, f.object.image || CatalogImage.new]
    f.inputs 'Image', for: [:image, f.object.build_image] do |fi|
      fi.input :file, as: :file, label: 'Image'
              #  hint: (f.template.image_tag(f.object.image&.url) rescue nil)
    end
  end
  f.actions
end

上传器(以前工作过,但甚至没有碰到过臭虫):

class ImageUploader < CarrierWave::Uploader::Base
  storage :file

  def store_dir
    byebug
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  def extension_white_list
    %w(jpg jpeg gif png)
  end

  def filename
    if original_filename
      original_filename.gsub! /\s*[^A-Za-z0-9\.\/]\s*/, '_'
      original_filename.strip
      original_filename.downcase
    end
  end
end

我检查了byebug ,确认其中有正确的模型,图像和文件字段。但是结果-表单没有任何错误,但再次显示了new表单。 如果我没有选择任何文件,它将保存文章。 (我也允许所有属性,因为它不允许所有必要)。

    [4, 13] in /usr/src/app/app/admin/articles.rb
    4:   # permit_params :name, :content, :position, :active,
    5:   #               image_attributes: [%i[id file _destroy]]
    6:
    7:   before_create do |article|
    8:     byebug
=>  9:   end
    10:
    11:   controller do
    12:     def permitted_params
    13:       params.permit!
(byebug) article.image.file
    #<ImageUploader:0x0000556fe6020ed0 @model=#<CatalogImage id: nil, imageable_type: "Article", imageable_id: nil, name: nil, file: nil, file_content_type: nil, position: nil, info: nil, created_at: nil, updated_at: nil>, @mounted_as=:file, @cache_id="1517464302-1-0005-0263", @filename="soldati-house-interior-by-victor-vasilev-o.jpg", @original_filename="soldati-house-interior-by-victor-vasilev-o.jpg", @file=#<CarrierWave::SanitizedFile:0x0000556fe741bde0 @file="/usr/src/app/public/uploads/tmp/1517464302-1-0005-0263/soldati-house-interior-by-victor-vasilev-o.jpg", @original_filename="soldati-house-interior-by-victor-vasilev-o.jpg", @content_type="image/jpeg">, @cache_storage=#<CarrierWave::Storage::File:0x0000556fe741ac60 @uploader=#<ImageUploader:0x0000556fe6020ed0 ...>>, @versions={:thumb=>#<ImageUploader::Uploader46969554945920:0x0000556fe70613f8 @model=#<CatalogImage id: nil, imageable_type: "Article", imageable_id: nil, name: nil, file: nil, file_content_type: nil, position: nil, info: nil, created_at: nil, updated_at: nil>, @mounted_as=:file, @parent_version=#<ImageUploader:0x0000556fe6020ed0 ...>, @parent_cache_id="1517464302-1-0005-0263", @cache_id="1517464302-1-0005-0263", @filename="soldati-house-interior-by-victor-vasilev-o.jpg", @original_filename="soldati-house-interior-by-victor-vasilev-o.jpg", @file=#<CarrierWave::SanitizedFile:0x0000556fe6adb370 @file="/usr/src/app/public/uploads/tmp/1517464302-1-0005-0263/thumb_soldati-house-interior-by-victor-vasilev-o.jpg", @original_filename="soldati-house-interior-by-victor-vasilev-o.jpg", @content_type="image/jpeg">, @cache_storage=#<CarrierWave::Storage::File:0x0000556fe6ff8650 @uploader=#<ImageUploader::Uploader46969554945920:0x0000556fe70613f8 ...>>, @versions={}>}>
    (byebug) n

    [74, 83] in /usr/local/bundle/gems/activeadmin-1.2.1/lib/active_admin/callbacks.rb
    74:           end
    75:
    76:           # def run_create_callbacks
    77:           define_method "run_#{name}_callbacks" do |*args, &block|
    78:             self.class.send("before_#{name}_callbacks").each{ |cbk| run_callback(cbk, *args) }
 => 79:             value = block.try :call
    80:             self.class.send("after_#{name}_callbacks").each { |cbk| run_callback(cbk, *args) }
    81:             return value
    82:           end
    83:           send :private, "run_#{name}_callbacks"
(byebug) c
(0.6ms)  BEGIN
             (0.3ms)  ROLLBACK
             Rendering /usr/local/bundle/gems/activeadmin-1.2.1/app/views/active_admin/resource/new.html.arb

Rails 5.1.4 ActiveAdmin 1.2.1

更新:

如果我尝试使用另一个扩展文件,则显示错误。

可以将has_one关联视为has_many 因此,这有效:

  f.object.build_image if f.object.image.nil?

  f.has_many :image, new_record: f.object.image.nil?, allow_destroy: true do |ff|
    ff.input :file, as: :file, hint: (ff.object&.file&.present? &&
                                      image_tag(ff.object&.file&.url(:thumb)))
    ff.input :file_cache, as: :hidden
  end

在这里,如果没有一个对象,我还将构建一个新对象,如果已经有一个对象,则不允许添加新对象。

更新,并且我必须向Image模型添加optional标志,因为在rails 5中,默认情况下需要此关联:

class CatalogImage < ApplicationRecord
  belongs_to :imageable, polymorphic: true, optional: true

在此之后它可以工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM