在视图中显示 Carrierwave 文件名

发布于 2024-10-19 07:56:54 字数 660 浏览 3 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

戏蝶舞 2024-10-26 07:56:55

Carrierwave 文档 可能有点偏离,但推荐的方法似乎是:

@page.form.file.identifier

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

@page.form.file.identifier
灯角 2024-10-26 07:56:55

@adamonduty 的解决方案很棒。我之前使用的另一个解决方案,只需在模型上创建一个方法:

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

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

def name
  file.path.split("/").last
end
格子衫的從容 2024-10-26 07:56:55

你是对的@epylinkn。文档指向使用:

@page.form.file.identifier

但是当我使用它时,我总是得到nil(正如@Cheng评论的那样)。

然后我检查了我的对象方法 (@page.form.file.methods.inspect),并发现以下方法可以工作:

@page.form.file_identifier

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
叹沉浮 2024-10-26 07:56:55

在模型的关联上传器类中,定义一个文件名方法。

def filename
  File.basename(path)
end

然后,您可以

model_instance.file.filename

从 CarrierWave 1.1.0 开始调用 Works。这是 kikito 和 Chris Alley 的上述回应的简洁重述/合并。

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.

霓裳挽歌倾城醉 2024-10-26 07:56:55

如果您使用 ActiveRecord,则可以通过两种方式直接访问名为 form 的字段:

def my_method
  self[:form]
end

def my_method
  form_before_type_cast
end

第二种方法是只读的。

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.

孤独陪着我 2024-10-26 07:56:55

CarrierWave::SanitizedFile 有一个私有的 original_filename 方法,其中包含上传文件的文件名。 (文档:http://rdoc.info/github/jnicklas/rierwave/ master/CarrierWave/SanitizedFile:original_filename)

通读此帖子后CarrierWave 邮件列表,似乎没有一个能满足我的需求。通过类似的操作,

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

我大量修改了原始文件名中的 :file 列值。因此,我决定在与 CarrierWave 绑定的单独一列中跟踪原始文件名。在我的 FileUploader 中,我只是添加了一个包装私有 original_filename 方法的阅读器:

def original_file
  original_filename
end

然后我向 Upload 添加了一个 before_create 事件code> class (我的 Upload 记录从未被修改,因此 before_create 可以满足我的需求)

before_create do
  self.original_file = self.file.original_file
end

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
红玫瑰 2024-10-26 07:56:55

我假设你有这样的模型?

class Page
  mount_uploader :form, FormUploader
end

如果是这样,您应该能够调用:

@page.form.url
@page.form.filename

您确定已正确上传/附加文件吗?当你检查@page.form时你会看到什么?请记住,在您完全处理完上传后,附件才会被保存。

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.

も星光 2024-10-26 07:56:55

这是我的解决方案:

  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

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
带上头具痛哭 2024-10-26 07:56:54

我已经能够通过 file 内部参数获取文件名:

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

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

<%= @page.form.file.filename %>
舞袖。长 2024-10-26 07:56:54

您正在查看的文档是经过清理的文件,它用于实际存储文件。您正在寻找的部分是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文