在视图中显示 Carrierwave 文件名
我正在尝试在 Rails erb 模板中显示 Carrierwave 附件的文件名。以下内容不起作用:
<%= @page.form.filename %>
这似乎符合文档。是否需要一些额外的步骤?
我的页面模型如下所示:
class Page < ActiveRecord::Base
mount_uploader :form, FormUploader
end
表单上传器如下所示:
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
Carrierwave 文档 可能有点偏离,但推荐的方法似乎是:
The Carrierwave docs might be a bit off, but recommended way seems to be:
@adamonduty 的解决方案很棒。我之前使用的另一个解决方案,只需在模型上创建一个方法:
@adamonduty's solution is great. Another solution I used before, just create a method on the model:
你是对的@epylinkn。文档指向使用:
但是当我使用它时,我总是得到
nil
(正如@Cheng评论的那样)。然后我检查了我的对象方法 (
@page.form.file.methods.inspect
),并发现以下方法可以工作:You're right @epylinkn. Documentation points towards using:
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:在模型的关联上传器类中,定义一个文件名方法。
然后,您可以
从 CarrierWave 1.1.0 开始调用 Works。这是 kikito 和 Chris Alley 的上述回应的简洁重述/合并。
In your model's associated uploader class, define a filename method.
You can then call
Works as of CarrierWave 1.1.0. This is a succinct restatement/amalgamation of kikito and Chris Alley's responses above.
如果您使用 ActiveRecord,则可以通过两种方式直接访问名为
form
的字段:或
第二种方法是只读的。
If you're using ActiveRecord, you can directly access the field named
form
in two ways:or
The second method is read-only.
CarrierWave::SanitizedFile
有一个私有的original_filename
方法,其中包含上传文件的文件名。 (文档:http://rdoc.info/github/jnicklas/rierwave/ master/CarrierWave/SanitizedFile:original_filename)通读此帖子后CarrierWave 邮件列表,似乎没有一个能满足我的需求。通过类似的操作,
我大量修改了原始文件名中的
:file
列值。因此,我决定在与 CarrierWave 绑定的单独一列中跟踪原始文件名。在我的FileUploader
中,我只是添加了一个包装私有original_filename
方法的阅读器:然后我向
Upload
添加了一个before_create
事件code> class (我的Upload
记录从未被修改,因此before_create
可以满足我的需求)CarrierWave::SanitizedFile
has a privateoriginal_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
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 myFileUploader
I simply added a reader that wraps the privateoriginal_filename
method:I then added a
before_create
event to theUpload
class (myUpload
records are never modified, so abefore_create
is acceptable for my needs)我假设你有这样的模型?
如果是这样,您应该能够调用:
您确定已正确上传/附加文件吗?当你检查@page.form时你会看到什么?请记住,在您完全处理完上传后,附件才会被保存。
I'm assuming you've got models like this?
If so you should be able to call:
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.
这是我的解决方案:
This is my solution:
我已经能够通过
file
内部参数获取文件名:I have been able to get the filename via the
file
internal parameter:您正在查看的文档是经过清理的文件,它用于实际存储文件。您正在寻找的部分是 FormUploader,它是一个上传器,并且是 http 的一部分://rubydoc.info/gems/rierwave/0.5.2/CarrierWave/Uploader
如果你想获取文件名,可以直接从数据库列中读取,也可以使用 File.basename (@page.form.path) 轻松提取它。
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.