Displaying a Carrierwave filename in the view

Ruby on-RailsRuby on-Rails-3Carrierwave

Ruby on-Rails Problem Overview


I am trying to display the filename of a Carrierwave attachment in a Rails erb template. The following does not work:

<%= @page.form.filename %>

This seems in line with the documentation. Is some additional step needed?

My page model looks like this:

class Page < ActiveRecord::Base

  mount_uploader :form, FormUploader

end

The form uploader looks like this:

class FormUploader < CarrierWave::Uploader::Base

  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  def extension_white_list
    %w(pdf)
  end

end

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

I have been able to get the filename via the file internal parameter:

<%= @page.form.file.filename %>

Solution 2 - Ruby on-Rails

The documentation you're looking at is the sanitized file, it's what it uses for actually storing a file. The part you're looking for is FormUploader, which is an Uploader, and part of http://rubydoc.info/gems/carrierwave/0.5.2/CarrierWave/Uploader

If you want to get the file name, you could either read it from the database column directly, or use File.basename(@page.form.path) to extract it easily.

Solution 3 - Ruby on-Rails

The Carrierwave docs might be a bit off, but recommended way seems to be:

@page.form.file.identifier

Solution 4 - Ruby on-Rails

@adamonduty's solution is great. Another solution I used before, just create a method on the model:

def name
  file.path.split("/").last
end

Solution 5 - Ruby on-Rails

You're right @epylinkn. Documentation points towards using:

@page.form.file.identifier

But when I use that, I always get nil (just as @Cheng commented).

I then inspected my objects methods (@page.form.file.methods.inspect), and found the following to work:

@page.form.file_identifier

Solution 6 - Ruby on-Rails

In your model's associated uploader class, define a filename method.

def filename
  File.basename(path)
end

You can then call

model_instance.file.filename

Works as of CarrierWave 1.1.0. This is a succinct restatement/amalgamation of kikito and Chris Alley's responses above.

Solution 7 - Ruby on-Rails

If you're using ActiveRecord, you can directly access the field named form in two ways:

def my_method
  self[:form]
end

or

def my_method
  form_before_type_cast
end

The second method is read-only.

Solution 8 - Ruby on-Rails

CarrierWave::SanitizedFile has a private original_filename method containing the filename of the uploaded file. (docs: http://rdoc.info/github/jnicklas/carrierwave/master/CarrierWave/SanitizedFile:original_filename)

After reading through this thread from the CarrierWave mailing list, none seemed to fit my needs. With something like

class Upload < ActiveRecord::Base
  mount_uploader :file, FileUploader
  # ...

I heavily modify the :file column value from the original filename. Due to this I decided to track the original filename in a separate column from the one bound to CarrierWave. In my FileUploader I simply added a reader that wraps the private original_filename method:

def original_file
  original_filename
end

I then added a before_create event to the Upload class (my Upload records are never modified, so a before_create is acceptable for my needs)

before_create do
  self.original_file = self.file.original_file
end

Solution 9 - Ruby on-Rails

I'm assuming you've got models like this?

class Page
  mount_uploader :form, FormUploader
end

If so you should be able to call:

@page.form.url
@page.form.filename

Are you sure you've uploaded/attached the file correctly? What do you see when you inspect @page.form? Remember, the attachment will not be saved until you've fully processed the upload.

Solution 10 - Ruby on-Rails

This is my solution:

  before_save :update_file_attributes


  def update_file_attributes
    if file.present? && file_changed? 
      self.content_type = file.file.content_type
      self.file_size = file.file.size
      self.file_name = read_attribute(:file)
    end
  end

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris AlleyView Question on Stackoverflow
Solution 1 - Ruby on-RailskikitoView Answer on Stackoverflow
Solution 2 - Ruby on-RailsZachary AnkerView Answer on Stackoverflow
Solution 3 - Ruby on-RailsepylinknView Answer on Stackoverflow
Solution 4 - Ruby on-RailsomarvelousView Answer on Stackoverflow
Solution 5 - Ruby on-RailsskplunkerinView Answer on Stackoverflow
Solution 6 - Ruby on-RailsSolomons_EcclesiastesView Answer on Stackoverflow
Solution 7 - Ruby on-RailsadamlamarView Answer on Stackoverflow
Solution 8 - Ruby on-RailsdeefourView Answer on Stackoverflow
Solution 9 - Ruby on-RailsWinfieldView Answer on Stackoverflow
Solution 10 - Ruby on-RailswhyView Answer on Stackoverflow