Rails Admin With Dragonfly - Edit. No file present
Asked Answered
C

1

7

Using Rails Admin with Dragonfly. However when I have created a new post with an attachment connected :ob to dragonfly and wants to edit it. It sais "No file chosen". As it doesn't pick up that there is already a file present?

In my rails_admin I have done this.

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

Here's my model:

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

Here's my schema:

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

Is there anything else that I need to do in order for it to pick up the file?

https://github.com/sferik/rails_admin

https://github.com/markevans/dragonfly

Confessedly answered 7/3, 2016 at 14:25 Comment(3)
Could you show the model and schema that uses Dragonfly and is this a custom view?Evidential
@Evidential done! No it's not a custome view.Confessedly
Can you also post the form present in view?Clarettaclarette
H
2

I managed to reproduce your issue using the configuration you provided and the fix that worked for me turned out to be incredibly simple: just remove the ob column from the documents table.

Explanation: by default, Dragonfly stores the attached documents on disk (in a file store) to the directory specified in the Dragonfly initializer. In the database, Dragonfly stores only the name and UID of the documents. In your case it's the ob_uid and ob_name columns that you correctly added to your schema.

So, unless you configured some custom store for the documents, I assume you use the default file store and the ob column is not needed. In fact, it confuses the rails_admin's dragonfly support code in such a way that, indeed, the edit page incorrectly show "No file chosen" all the time.

Adding an image after the fix (for simplicity, I removed the document_categories association from both the model and the edit action in rails_admin):

Adding an image after the fix

Hyacinthe answered 30/3, 2016 at 21:13 Comment(4)
"The BountyHunter"Maldon
@Brad, I know, I just couldn't resist this one. But I'll back off for a few days now :)Pesade
Please don't, on my account - I think it's awesome! You're helping a lot of people with some tough questions.Maldon
No no, I'll be just almost off the internet for a while... And thanks!Pesade

© 2022 - 2024 — McMap. All rights reserved.