可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
回答1:
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.
回答2:
I have been able to get the filename via the file
internal parameter:
<%= @page.form.file.filename %>
回答3:
The Carrierwave docs might be a bit off, but recommended way seems to be:
@page.form.file.identifier
回答4:
@adamonduty's solution is great. Another solution I used before, just create a method on the model:
def name
file.path.split("/").last
end
回答5:
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
回答6:
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.
回答7:
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.
回答8:
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.
回答9:
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
回答10:
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