Cómo reasignar archivos cargados a algunos otros modelos rails 3.1

I have a scenario where i am uploading files dynamically using ajax remotipart, then later i assign those uploaded files to any model. I have such settings in uploader.

class DocumentUploader < CarrierWave::Uploader::Base
  storage :file
  def store_dir
    "uploads/#{model.class.to_s.pluralize.underscore}/#{model.documentable_type.to_s.pluralize.underscore}/#{model.documentable_id}"
  end

  def extension_white_list
    %w(doc pdf docx xls xlsx)
  end
end

class Document < ActiveRecord::Base
  mount_uploader :document, DocumentUploader
  belongs_to :documentable, :polymorphic => true

  validates :document, :presence => true
  validates :description, :presence => true
end

class Post < ActiveRecord::Base
    has_many :documents, :as=>:documentable
end

When i upload a document without providing documentable source, it uploads the files to /uploads/documents/uploaded_file.doc

But when i assign that document to some other model like

@post = Post.first
@post.documents << Document.last
@post.save

It save the record correctly and when get url of the file like @post.documents.first.document.url it gave url like this /uploads/documents/posts/10212/uploaded_file.doc where the file was not available.

How can i handle such assignment of pre-existing uploads?

preguntado el 01 de febrero de 12 a las 14:02

2 Respuestas

I came here while googling and thought some people might help this piece of code I'm using in a controller to re-assign a file (here it's an image) to another model which is handled with CarrierWave:

def create
  temporary_image = TemporaryImage.find(params[:temporary_image][:id])
  @player = Player.new(params[:player])
  @player.image = File.open(temporary_image.image.current_path)
  # now you can handle @player.image as if it was
  # originally assigned by CarrierWave
end

dónde

class Player < ActiveRecord::Base
  attr_accessible :image
  mount_uploader :image, PlayerImageUploader
end

y la

class TemporaryImage < ActiveRecord::Base
  attr_accessible :image
  mount_uploader :image, TemporaryImageUploader
end

I'm using this in conjunction with an ajax Subir archivo (stored via TemporaryImage) where the image is immediately presented and can then be recortada. After cropping it is stored within Player.

Respondido 05 Oct 12, 15:10

I solved the issue by doing lot of hit and trail. but finally solved.

class Document < ActiveRecord::Base
  mount_uploader :document, DocumentUploader
  belongs_to :documentable, :polymorphic => true

  validates :document, :presence => true
  validates :description, :presence => true

  after_update :relocate_files

  def relocate_files
    src_file = Rails.root.join("public", "uploads", "documents", document.url.split("/").last)
    dst_file = Rails.root.join("public", "uploads", "documents", documentable_type.pluralize.underscore, documentable_id.to_s, document.url.split("/").last)
    unless FileTest.exists?(dst_file)
      FileUtils.mkdir_p(File.dirname(dst_file))
      FileUtils.mv(src_file, dst_file)
    end
  end
end

Respondido 03 Feb 12, 01:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.