Rails管理员使用Dragonfly - 编辑。没有档案

时间:2016-03-07 14:25:02

标签: ruby-on-rails rails-admin dragonfly-gem

Rails AdminDragonfly一起使用。但是,当我创建了一个新帖子,附件:ob连接到dragonfly并想要编辑它。这是“没有选择文件”。因为它没有拿起已存在的文件?

在我的rails_admin中,我已经完成了这个。

edit do
  field :name
  field :information
  field :ob, :dragonfly
  field :document_categories
end

这是我的模特:

class Document < ActiveRecord::Base
  has_and_belongs_to_many :document_categories


  after_commit :generate_versions, on: :create
  dragonfly_accessor :ob


  validates :name, :ob, presence: true

  def generate_versions
    DocumentWorker.perform_async(self.id)
  end

  def convertable_image?
    unless self.try(:ob).nil?
      self.try(:ob).mime_type.include?("image") || self.try(:ob).mime_type.include?("pdf")
    else
      return false
    end
  end

  def respond_with_type
    case self.try(:ob).mime_type.split("/")[1]
      when "vnd.ms-powerpoint" , "vnd.openxmlformats-officedocument.presentationml.presentation", "application/vnd.openxmlformats-officedocument.presentationml.template"
        "powerpoint"
      when "application/vnd.ms-excel" , "vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        "excel"       
      when "application/msword" , "vnd.openxmlformats-officedocument.wordprocessingml.document"
        "word"                
      else
        self.try(:ob).mime_type.split("/")[1]
      end
  end  
  default_scope{order("name ASC")}
end

这是我的架构:

create_table "documents", force: :cascade do |t|
    t.string   "name"
    t.string   "ob"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.string   "ob_uid"
    t.string   "ob_name"
    t.text     "information"
  end

为了获取文件,还有什么我需要做的吗?

https://github.com/sferik/rails_admin

https://github.com/markevans/dragonfly

1 个答案:

答案 0 :(得分:2)

我设法使用您提供的配置重现您的问题,并且对我有用的修补程序非常简单:只需ob表中删除documents

说明:默认情况下,Dragonfly将附加的文档存储在磁盘上(在文件存储中)到Dragonfly initializer中指定的目录。在数据库中,Dragonfly仅存储文档的名称和UID 。在您的情况下,它是您正确添加到架构中的ob_uidob_name列。

因此,除非您为文档配置了一些custom store,否则我假设您使用默认文件存储,并且不需要ob列。实际上,它会使rails_admin dragonfly support code混淆,实际上,编辑页面错误地显示&#34;没有选择文件&#34;一直以来。

修复后添加图像(为简单起见,我从rails_admin中的模型和编辑操作中删除了document_categories关联):

Adding an image after the fix