Chris Alley
Chris Alley

Reputation: 3035

Displaying a Carrierwave filename in the view

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

Upvotes: 55

Views: 56143

Answers (10)

Solomons_Ecclesiastes
Solomons_Ecclesiastes

Reputation: 2099

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.

Upvotes: 6

omarvelous
omarvelous

Reputation: 2784

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

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

Upvotes: 9

skplunkerin
skplunkerin

Reputation: 2373

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

Upvotes: 6

epylinkn
epylinkn

Reputation: 1128

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

@page.form.file.identifier

Upvotes: 17

why
why

Reputation: 24851

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

Upvotes: 1

Zachary Anker
Zachary Anker

Reputation: 4520

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.

Upvotes: 81

kikito
kikito

Reputation: 52651

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

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

Upvotes: 87

adamlamar
adamlamar

Reputation: 4927

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.

Upvotes: 4

deefour
deefour

Reputation: 35360

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

Upvotes: 3

Winfield
Winfield

Reputation: 19145

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.

Upvotes: 2

Related Questions