Display Carrierwave media name in view

I am trying to display the name of a Carrierwave application file in an erb Rails template. The following does not work:

<%= @page.form.filename %> 

This is similar to the documentation . Is an extra step required?

My page model is as follows:

 class Page < ActiveRecord::Base mount_uploader :form, FormUploader end 

The user loading the form is as follows:

 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 
+44
ruby-on-rails ruby-on-rails-3 carrierwave
Feb 27 '11 at 11:07
source share
10 answers

The documentation you are looking at is a sanitized file, this is what it uses to actually store the file. The part you are looking for is FormUploader, which is Uploader, and the http://rubydoc.info/gems/carrierwave/0.5.2/CarrierWave/Uploader part

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

+76
Feb 28 '11 at 2:44
source share

I managed to get the file name through the internal parameter file :

 <%= @page.form.file.filename %> 
+70
May 7 '12 at
source share

Carrierwave docs may be turned off a bit, but the recommended method is as follows:

 @page.form.file.identifier 
+14
Sep 06 '13 at 3:08 on
source share

@Adamonduty's solution is great. Another solution that I used before, just create a method for the model:

 def name file.path.split("/").last end 
+6
Jul 02 '12 at 7:13
source share

You're right @epylinkn. The documentation indicates the use of:

 @page.form.file.identifier 

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

Then I looked at the methods of my objects ( @page.form.file.methods.inspect ) and found the following:

 @page.form.file_identifier 
+6
Jul 18 '14 at 17:29
source share

If you use ActiveRecord, you can directly access a field named form two ways:

 def my_method self[:form] end 

or

 def my_method form_before_type_cast end 

The second method is read-only.

+4
Mar 16 2018-12-12T00:
source share

I assume you have such models?

 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 uploaded / entered the file correctly? What do you see when you check @ page.form? Remember that the attachment will not be saved until you fully process the download.

+3
Feb 27 2018-11-17T00:
source share

In the loader class associated with the model, define the file name method.

 def filename File.basename(path) end 

Then you can call

 model_instance.file.filename 

Works with CarrierWave 1.1.0. This is a brief repetition / merger of the answers of kikito and Chris Alley above.

+2
Sep 02 '17 at 0:51
source share

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

After reading this thread from the CarrierWave mailing list, none of them matched my needs. Something like

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

I am strongly changing the value of the :file column from the original file name. In this regard, I decided to track the original file name in a separate column from the only one associated with CarrierWave. In my FileUploader I just added a reader that wraps the private method original_filename :

 def original_file original_filename end 

Then I added the before_create event to the Upload class (my Upload records never changed, so before_create is suitable for my needs)

 before_create do self.original_file = self.file.original_file end 
+1
Apr 30 '11 at 13:45
source share

This is my decision:

  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 
+1
Apr 21 '13 at 9:30
source share



All Articles