在视图中显示Carrierwave文件名 [英] Displaying a Carrierwave filename in the view

查看:84
本文介绍了在视图中显示Carrierwave文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Rails erb模板中显示Carrierwave附件的文件名。以下内容不起作用:

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?

我的页面模型如下:

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


推荐答案

您正在查看的文档是经过清理的文件,它就是它用于实际存储文件。您要找的部分是FormUploader,它是一个上载器,并且是 http的一部分://rubydoc.info/gems/carrierwave/0.5.2/CarrierWave/Uploader

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

如果要获取文件名,则可以阅读直接从数据库列中提取,或使用 File.basename(@ page.form.path)轻松提取。

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.

这篇关于在视图中显示Carrierwave文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆