Rails Admin和Dragonfly - 编辑。没有文件存在。

7
使用 Rails AdminDragonfly。但是当我创建了一个带有连接到 Dragonfly 的附件 :ob 的新文章并想要编辑它时,它显示“未选择文件”。因为它没有识别出已经存在一个文件吗?
在我的 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
你能展示一下使用Dragonfly的模型和架构吗?这是自定义视图吗? - SacWebDeveloper
@SacWebDeveloper 完成了!不,这不是一个自定义视图。 - Philip
您能否也在视图中发布“form”表单? - Sahil
1个回答

2
我使用你提供的配置成功重现了你的问题,而解决方法非常简单: 只需从“documents”表中删除“ob”列。
解释:默认情况下,Dragonfly将附加的文档存储在磁盘上(在文件存储库中),并将其存储到“Dragonfly initializer”中指定的目录中。在数据库中,Dragonfly仅存储文档的名称和UID。在你的情况下,你正确地将其添加到了模式中的“ob_uid”和“ob_name”列。
因此,除非你为文件配置了一些自定义存储库,否则我假设你使用默认的文件存储库,并且不需要“ob”列。事实上,它会以一种混淆 “rails_admin”的“dragonfly支持代码”的方式来混淆,使编辑页面始终不正确地显示“未选择文件”。
修复后添加图像(为简单起见,我从模型和“rails_admin”中的edit操作中删除了“document_categories”关联):

赏金猎人 - Brad Werth
@Brad,我知道,我只是忍不住了。但是现在我会退一步几天 :) - Matouš Borák
请不要因为我而停止 - 我认为这很棒!你正在帮助许多人解决一些困难的问题。 - Brad Werth
不,我只是要暂时离开互联网一段时间...谢谢! - Matouš Borák

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接