1

我不知道我做了什么或发生了什么变化,因为这以前有效。

我有一个模型Entry,我使用 Paperclip 将文件附加document到它。现在,由于某种奇怪的原因,我不断收到

Errno::ENOENT in EntriesController#create
No such file or directory - /var/www/capsf-web/public/assets/entries/test.pdf

我猜在 Paperclip 将文件保存到目录之前,我已经在尝试对文件进行编码。这是Entry看起来的样子。我正在使用 ElasticSearch 的附件映射器,这就是我对其进行编码的原因。

class Entry < ActiveRecord::Base
  include Tire::Model::Search
  include Tire::Model::Callbacks

  has_and_belongs_to_many :categories
  has_and_belongs_to_many :subcategories
  belongs_to :entry_type
  has_attached_file :document,
                    :url  => "/assets/entries/:basename.:extension",
                    :path => ":rails_root/public/assets/entries/:basename.:extension"

  before_post_process :image?

  validates_presence_of :entry_type

  attr_accessible :description, :title, :url, :category_ids, :subcategory_ids, :entry_type_id, :document

  mapping do
    indexes :title
    indexes :description
    indexes :categories do
      indexes :name
    end
    indexes :subcategories do
      indexes :name
    end
    indexes :entry_type
    indexes :document, :type => 'attachment'
  end

  def to_indexed_json
   #to_json( methods: [:category_name, :subcategory_name, :entry_type_name])
    {
    :title          => title,
    :description    => description,
    :categories     => categories.map { |c| { :name => c.name}},
    :subcategories  => subcategories.map { |s| { :name => s.name}},
    :entry_type     => entry_type_name,
    :document       => attachment
    }.to_json
  end
  def image?
    !(document_content_type =~ /^image.*/).nil?
  end

  def attachment
    if document.present?
      path_to_document = Rails.public_path+"/assets/entries/#{document_file_name}"
      Base64.encode64(open(path_to_document) { |pdf| pdf.read})
#If I comment out the line above everything works just fine.

    end
  end


end
4

0 回答 0